有固定好的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}
]
}
]
}
}
}
如果需要拿来用,肯定要反序列化,序列化成实体,结构如下(一般情况下你会手写的,大神除外):
publicclassRootobject
{publicItems_Custom_Get_Response items_custom_get_response {get;set; }
}publicclassItems_Custom_Get_Response
{publicItems items {get;set; }
}publicclassItems
{publicList<Item> item {get;set; }
}publicclassItem
{publicintnum_iid {get;set; }publicintproduct_id {get;set; }publicList<Sku> skus {get;set; }
}publicclassSku
{publicobjectcreated {get;set; }publicobjectmodified {get;set; }publicobjectouter_id {get;set; }publicobjectprice {get;set; }publicobjectproperties {get;set; }publicstringproperties_name {get;set; }publicstringquantity {get;set; }publicobjectsku_id {get;set; }
}
写完这些你是不是觉得自己蒙蒙哒??
楼主给你推荐一个快速生成的方法,使用VS2013或者2015,好像VS2012 这个不支持! 怎样快速生成对应的实体呢?
复制json字符串,然后选择将JSON粘贴为类。
[flash=500,400]http://s1.knowsky.com/20160211/iech3nfwjfn17.jpg[/flash]
然后就在类文件中生成下面文件:
publicclassRootobject
{publicItems_Custom_Get_Response items_custom_get_response {get;set; }
}publicclassItems_Custom_Get_Response
{publicItems items {get;set; }
}publicclassItems
{publicItem[] item {get;set; }
}publicclassItem
{publicintnum_iid {get;set; }publicintproduct_id {get;set; }publicSku[] skus {get;set; }
}publicclassSku
{publicobjectcreated {get;set; }publicobjectmodified {get;set; }publicobjectouter_id {get;set; }publicobjectprice {get;set; }publicobjectproperties {get;set; }publicstringproperties_name {get;set; }publicstringquantity {get;set; }publicobjectsku_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的强大功能还在探索中。。。。。
[flash=500,400]http://s1.knowsky.com/20160211/pafpj42np1a17.jpg[/flash]
源自:http://www.cnblogs.com/viaiu/p/5007644.html