物件與Json格式間的轉換

  • 3305
  • 0
  • 2015-12-28

摘要:[.NET][C#] 物件與Json格式間的轉換

一. 使用 System.Web.Script.Serialization;

    
    // 實作JsonConverter
    using System.Web.Script.Serialization;
    public static class JsonConverter
    {
        public static string EntityToJson<T>(T entity)
        {
            JavaScriptSerializer JSONSerializer = new JavaScriptSerializer();
            return JSONSerializer.Serialize(entity);
        }

        public static object JsonToEntity<T>(string json)
        {
            JavaScriptSerializer JSONSerializer = new JavaScriptSerializer();

            //return JSONSerializer.Deserialize(json, typeof(T));
            // or
            return JSONSerializer.Deserialize<T>(json);
        }

    }



 

二. 使用 Newtonsoft.Json.dll

1. 必須先下載 Newtonsoft.Json.dll 並加入Reference

2.

    //實作JsonConverter
    public static class JsonConverter
    {
        public static T ToEntity<T>(string jsonString)
        {
            return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(jsonString);
        }


        public static string ToJsonString<T>(T entity)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(entity);
        }
    }

 

三. 測試

    // 測試
    public class test
    {
        public void main()
        {
            Category entity = initData();

            // value of jsonString will same as jsonString2
            string jsonString = JsonConverter.EntityToJson(entity);
            string jsonString2 = JsonConverter.EntityToJson<Category>(entity);

            object deserializeResult = JsonConverter.JsonToEntity<Category>(jsonString);

        }

        // 初始化測試資料
        public Category initData()
        {
            List<Item> items = new List<Item>();
            for (int i = 1; i < 20; i++)
            {
                items.Add(new Item(i.ToString(), "item" + i.ToString(), "description" + i.ToString()));
            }
            Category cate = new Category("1", "cate1", items);
            return cate;
        }
    }

    public class Category
    {
        public string ID { get; set; }
        public string categoryName { get; set; }
        public List<Item> items { get; set; }

        public Category()
        {
            this.items = new List<Item>();
        }

        public Category(string id, string name, List<Item> items)
        {
            this.ID = id;
            this.categoryName = name;
            this.items = new List<Item>();
            if (items != null)
            {
                foreach (var item in items)
                {
                    this.items.Add(item);
                }
            }
        }
    }
    
    public class Item
    {
        public string ID { get; set; }
        public string itemName { get; set; }
        public string itemDescription { get; set; }

        public Item()
        {

        }

        public Item(string id, string name, string description)
        {
            this.ID = id;
            this.itemName = name;
            this.itemDescription = description;
        }
    }