json 的序列化與反序列化 - 使用 Newtonsoft 與 JavaScriptSerializer

 

Newtonsoft.Json.JsonConvert與System.Web.Script.Serialization.JavaScriptSerializer

皆可用來做json的序列化(物件轉json格式)與反序列化(json格式轉物件),

而Json.NET宣稱 JsonConvert 的效能比 JavaScriptSerializer 轉得還快,

以下個別列出使用方式。

 

一、Newtonsoft.Json.JsonConvert的序列化

Model為

using Newtonsoft.Json;

namespace WebApplication1.Models
{
    public class Person
    {
        [JsonProperty("i-d")]
        public int id { set; get; }
        public string name { set; get; }

        public Person(int id, string name)
        {
            this.id = id;
            this.name = name;
        }
    }
}

Controller為

using Newtonsoft.Json;
using System.Collections.Generic;
using System.Web.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        static List<Person> PersonCollection = new List<Person>() { new Person(1, "Tim"), new Person(2, "Brooke"), new Person(3, "Mary") };
        public ActionResult Index()
        {
            string Serialize = JsonConvert.SerializeObject(PersonCollection);

            return Content(Serialize);
        }
    }
}

 

View不需改動。

結果為

[{"i-d":1,"name":"Tim"},{"i-d":2,"name":"Brooke"},{"i-d":3,"name":"Mary"}]

 

注意Model方面我故意用了JsonPropertyAttribute屬性(黃色框框),

用以表示在資料傳遞方面json格式使用帶有「i-d」的字串傳遞,而在程式內部處理時使用「id」字串處理。

下面的 Newtonsoft.Json.JsonConvert的反序列化 例子更能了解 JsonPropertyAttribute propertyName用法。

 

二、Newtonsoft.Json.JsonConvert的反序列化

Model同上。

Controller為

using Newtonsoft.Json;
using System.Collections.Generic;
using System.Web.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult About()
        {
            //一序列化字串
            string Serialize = @"[{""i-d"":1,""name"":""Tim""},{""i-d"":2,""name"":""Brooke""},{""i-d"":3,""name"":""Mary""}]";

            //反序列化
            List<Person> PersonCollection2 = JsonConvert.DeserializeObject<List<Person>>(Serialize);

            return View(PersonCollection2);
        }
    }
}

view為

@model  List<WebApplication1.Models.Person>
@using WebApplication1.Models

@{
    ViewBag.Title = "About";
}

<h2>@ViewBag.Title.</h2>

<table style="border-collapse:separate;border: 1px solid;">
    <tbody>
        @{
            <tr>
                <th style="border-collapse:separate;border: 1px solid;">id</th>
                <th style="border-collapse:separate;border: 1px solid;">name</th>
            </tr>
            foreach (Person item in Model)
            {
                <tr>
                    <td style="border-collapse:separate;border: 1px solid;">@item.id</td>
                    <td style="border-collapse:separate;border: 1px solid;">@item.name</td>
                </tr>
            }
        }
    </tbody>
</table>

結果為

 

三、System.Web.Script.Serialization的序列化

Model為

namespace WebApplication1.Models
{
    public class Person
    {
        public int id { set; get; }
        public string name { set; get; }

        public Person(int id, string name)
        {
            this.id = id;
            this.name = name;
        }
    }
}

Controller為

using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.Web.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        static List<Person> PersonCollection = new List<Person>() { new Person(1, "Tim"), new Person(2, "Brooke"), new Person(3, "Mary") };
        public ActionResult Index()
        {
            JavaScriptSerializer JavaScriptSerializer = new JavaScriptSerializer();
            string Serialize = JavaScriptSerializer.Serialize(PersonCollection);

            return Content(Serialize);
        }
    }
}

View不需改動。

結果為

 

四、System.Web.Script.Serialization的反序列化

Model請再添加「無參數建構函式」如下,

namespace WebApplication1.Models
{
    public class Person
    {
        public int id { set; get; }
        public string name { set; get; }

        public Person() { }

        public Person(int id, string name)
        {
            this.id = id;
            this.name = name;
        }
    }
}

否則在做反序列化時會出錯如下

Controller為

using System.Web.Script.Serialization;
using System.Collections.Generic;
using System.Web.Mvc;
using WebApplication1.Models;

namespace WebApplication1.Controllers
{
    public class HomeController : Controller
    {
        static List<Person> PersonCollection = new List<Person>() { new Person(1, "Tim"), new Person(2, "Brooke"), new Person(3, "Mary") };

        public ActionResult About()
        {
            //序列化
            JavaScriptSerializer JavaScriptSerializer = new JavaScriptSerializer();
            string Serialize = JavaScriptSerializer.Serialize(PersonCollection);

            //反序列化
            List<Person> PersonCollection2 = JavaScriptSerializer.Deserialize<List<Person>>(Serialize);

            return View(PersonCollection2);
        }
    }
}

View不需改動。

結果為

 

參考資料:

https://www.newtonsoft.com/json

JavaScriptSerializer Class

DataContractJsonSerializer Class

[C#] 使用JavasacriptSerializer序列化Entity為Json

[C#.net] 產生JSON字串的幾種方式整理