2013年6月18日 星期二

JavaScriptSerializer 轉換 IEnumerable 介面

一樣開新Console專案做示範,並加入System.Web.Extensions.dll參考
完整的程式碼如下
using System;
using System.Collections.Generic;
using System.Web.Script.Serialization;

namespace JavaScriptSerializerDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            // 淮備要序列化的資料
            List<JsonObject> data = new List<JsonObject>()
            {
                new JsonObject()
                {
                    ID = 1,
                    Name = "JsonObject1",
                    State = EnumState.a,
                    Date = DateTime.Today.AddDays(-1),
                    GUID = Guid.NewGuid()
                },
                new JsonObject()
                {
                    ID = 2,
                    Name = "JsonObject2",
                    State = EnumState.b,
                    Date = DateTime.Today,
                    GUID = Guid.NewGuid()
                },
                new JsonObject()
                {
                    ID = 3,
                    Name = "JsonObject3",
                    State = EnumState.c,
                    Date = DateTime.Today.AddDays(1),
                    GUID = Guid.NewGuid()
                },
            };

            // 序列化
            JavaScriptSerializer jss = new JavaScriptSerializer();
            string jsonString = jss.Serialize(data);
            Console.WriteLine("序列化的JSON字串:{0}\r\n", jsonString);

            // 反序列化
            List<JsonObject> obj = jss.Deserialize<List<JsonObject>>(jsonString);
            foreach (var item in obj)
            {
                Console.WriteLine(
                    "反序列化後的物件, ID:{0}, Name:{1}, State:{2}, Date:{3}, GUID:{4}",
                    item.ID,
                    item.Name,
                    item.State,
                    item.Date,
                    item.GUID);
            }

            Console.ReadLine();
        }

        /// <summary>
        /// 要序列化的資料類別
        /// </summary>
        private class JsonObject
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public EnumState State { get; set; }
            public DateTime Date { get; set; }
            public Guid GUID { get; set; }
        }

        /// <summary>
        /// 狀態列舉
        /// </summary>
        public enum EnumState
        {
            a = 0,
            b = 1,
            c = 2
        }
    }
}

序列化後的結果
[
  {
    "ID": 1,
    "Name": "JsonObject1",
    "State": 0,
    "Date": "\/Date(13713984 00000)\/",
    "GUID": "97eff121-b03d-4c76-9d18-1a82660f3f40"
  },
  {
    "ID": 2,
    "Name": "JsonObject2",
    "State": 1,
    "Date": "\/Date(1371484800000)\/",
    "GUID": "2fa2f230-d721-4c21-9630 -a90657bb4aac"
  },
  {
    "ID": 3,
    "Name": "JsonObject3",
    "State": 2,
    "Date": "\/Date(1371571200 000)\/",
    "GUID": "e8a8462f-7c59-4f34-a895-484f2be13bd7"
  }
]
執行的結果