·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> VS快速生成JSON数据格式对应的实体

VS快速生成JSON数据格式对应的实体

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

   有固定好的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的强大功能还在探索中。。。。。        源自:http://www.cnblogs.com/viaiu/p/5007644.html