VS快速生成JSON數據格式對應的實體


      有固定好的Json數據格式,你還在手動敲對應的實體嗎?有點low了!步入正題,這是一個json字符串,先去驗證JSON數據格式( http://www.bejson.com/)如下:
{
    "items_custom_get_response": {
        "items": {
            "item": [
                {
                    "num_iid": 1,
                    "product_id": 0,
                    "skus": [
                        {
                            "created": null,
                            "modified": null,
                            "outer_id": null,
                            "price": null,
                            "properties": null,
                            "properties_name": "黑色",
                            "quantity": "2",
                            "sku_id": null
                        }
                    ]
                }
            ]
        }
    }
}

 

      如果需要拿來用,肯定要反序列化,序列化成實體,結構如下(一般情況下你會手寫的,大神除外):

  public class Rootobject
        {
            public Items_Custom_Get_Response items_custom_get_response { get; set; }
        }
 
        public class Items_Custom_Get_Response
        {
            public Items items { get; set; }
        }
 
        public class Items
        {
            public List<Item> item { get; set; }
        }
 
        public class Item
        {
            public int num_iid { get; set; }
            public int product_id { get; set; }
            public List<Sku> skus { get; set; }
        }
 
        public class Sku
        {
            public object created { get; set; }
            public object modified { get; set; }
            public object outer_id { get; set; }
            public object price { get; set; }
            public object properties { get; set; }
            public string properties_name { get; set; }
            public string quantity { get; set; }
            public object sku_id { get; set; }
        } 

       寫完這些你是不是覺得自己蒙蒙噠??

      樓主給你推薦一個快速生成的方法,使用VS2013或者2015,好像VS2012 這個不支持! 怎樣快速生成對應的實體呢?
復制json字符串,然后選擇將JSON粘貼為類。
 
 
      然后就在類文件中生成下面文件: 
   public class Rootobject
        {
            public Items_Custom_Get_Response items_custom_get_response { get; set; }
        }
 
        public class Items_Custom_Get_Response
        {
            public Items items { get; set; }
        }
 
        public class Items
        {
            public Item[] item { get; set; }
        }
 
        public class Item
        {
            public int num_iid { get; set; }
            public int product_id { get; set; }
            public Sku[] skus { get; set; }
        }
 
        public class Sku
        {
            public object created { get; set; }
            public object modified { get; set; }
            public object outer_id { get; set; }
            public object price { get; set; }
            public object properties { get; set; }
            public string properties_name { get; set; }
            public string quantity { get; set; }
            public object sku_id { get; set; }
        } 
 

      它里面可能和自己定義的有點不一樣,那對象數組和泛型list有什么區別呢?

      數組有很多的優點,比如說數組在內存中是連續存儲的,所以它的索引速度是非常的快,而且賦值與修改元素也很簡單;ArrayList是.Net Framework提供的用於數據存儲和檢索的專用類,它是命名空間System.Collections下的一部分。它的大小是按照其中存儲的數據來動態擴充與收縮的。所以,我們在聲明ArrayList對象時並不需要指定它的長度。ArrayList繼承了IList接口,所以它可以很方便的進行數據的添加,插入和移除;正是因為ArrayList存在不安全類型與裝箱拆箱的缺點,所以在C#2.0后出現了泛型的概念。而List類是ArrayList類的泛型等效類。它的大部分用法都與ArrayList相似,因為List類也繼承了IList接口。最關鍵的區別在於,在聲明List集合時,我們同時需要為其聲明List集合內數據的對象類型。
      機器上裝了的有12、13、15,VS2015的強大功能還在探索中。。。。。
    
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM