问题描述
{"response":{"errorCount":0,"orderList":{"order":[{"orderAmount":119,"orderCode":"120914FAYKSR","orderCreateTime":"2012-09-1416:46:21","orderDeliveryFee":0,"orderId":75483957,"orderNeedInvoice":2,"orderStatus":"ORDER_TRUNED_TO_DO","productAmount":119,"updateTime":"2012-09-1417:05:13"},{"orderAmount":119,"orderCode":"120914TB23GA","orderCreateTime":"2012-09-1423:51:25","orderDeliveryFee":0,"orderId":75566567,"orderNeedInvoice":2,"orderStatus":"ORDER_TRUNED_TO_DO","productAmount":119,"updateTime":"2012-09-1500:08:14"}]},"totalCount":2}}C#怎么把这段JSON数据解析为order对象,然后更新到数据库中啊,求教能写个示例吗
解决方案
解决方案二:
正则表达式
解决方案三:
解决方案四:
C#解析JSON的方法:varjson={"你的JSON"};varorders=json.Deserialize<Order>(json);
解决方案五:
varjson={"response":{"errorCount":0,"orderList":{"order":[{"orderAmount":119,"orderCode":"120914FAYKSR","orderCreateTime":"2012-09-1416:46:21","orderDeliveryFee":0,"orderId":75483957,"orderNeedInvoice":2,"orderStatus":"ORDER_TRUNED_TO_DO","productAmount":119,"updateTime":"2012-09-1417:05:13"},{"orderAmount":119,"orderCode":"120914TB23GA","orderCreateTime":"2012-09-1423:51:25","orderDeliveryFee":0,"orderId":75566567,"orderNeedInvoice":2,"orderStatus":"ORDER_TRUNED_TO_DO","productAmount":119,"updateTime":"2012-09-1500:08:14"}]},"totalCount":2}};varorders=json.Deserialize<Order>(json);
解决方案六:
引用3楼的回复:
C#解析JSON的方法:C#codevarjson={"你的JSON"};varorders=json.Deserialize<Order>(json);
兄弟,这样还是不行啊
解决方案七:
请参考
解决方案八:
http://json.codeplex.com/
解决方案九:
定义一个和你json里一样的示例,主要是字段一样
解决方案十:
引用6楼的回复:
请参考C#Json转对象
哥们,还是没用啊,我这个格式跟那个不一样{"response":{"errorCount":0,"orderList":{"order":[{"orderAmount":119,"orderCode":"120914FAYKSR","orderCreateTime":"2012-09-1416:46:21","orderDeliveryFee":0,"orderId":75483957,"orderNeedInvoice":2,"orderStatus":"ORDER_TRUNED_TO_DO","productAmount":119,"updateTime":"2012-09-1417:05:13"},{"orderAmount":119,"orderCode":"120914TB23GA","orderCreateTime":"2012-09-1423:51:25","orderDeliveryFee":0,"orderId":75566567,"orderNeedInvoice":2,"orderStatus":"ORDER_TRUNED_TO_DO","productAmount":119,"updateTime":"2012-09-1500:08:14"}]},"totalCount":2}}
解决方案十一:
引用2楼的回复:
http://json.codeplex.com/http://msdn.microsoft.com/zh-cn/library/system.runtime.serialization.json.datacontractjsonserializer(v=vs.100).aspx
兄弟,这个方便弄一个示例吗?
解决方案十二:
http://www.cnblogs.com/pen-ink/archive/2011/10/06/2199715.html这个推荐一下。希望能帮助你。
解决方案十三:
引用2楼的回复:
http://json.codeplex.com/http://msdn.microsoft.com/zh-cn/library/system.runtime.serialization.json.datacontractjsonserializer(v=vs.100).aspx
哥们,方便弄一个demo吗,这种JSON格式的还是有点没搞明白
解决方案十四:
支持2楼的呢
解决方案十五:
你这个是嵌套的结构定义一个response类有errorCount,orderList,totalCount3个属性其中orderList是List<order>定义order类有:":"orderAmount":119,"orderCode":"120914FAYKSR","orderCreateTime":"2012-09-1416:46:21","orderDeliveryFee":0,"orderId":75483957,"orderNeedInvoice":2,"orderStatus":"ORDER_TRUNED_TO_DO","productAmount":119,"updateTime":"2012-09-1417:05:13这中的属性就OK了
解决方案:
protectedvoidPage_Load(objectsender,EventArgse){varjdata="[{"id":0,"email":"abccd","age":0},{"id":1,"email":"abc1","age":2}]";//stringjsonData=context.Request.Form["jdata"];//取得这个json字符串stringjsonData=jdata;//取得这个json字符串stringdatastr="";//调用上面这个方法,把json字符串传进去,就得到一个对象集合了List<Person>DataReust=JSONToObject<List<Person>>(jsonData);for(inti=0;i<DataReust.Count;i++){intid=DataReust[i].id;stringemail=DataReust[i].email;intage=DataReust[i].age;datastr+="id:"+id+"email:"+email+"age:"+age+"";}}//做一个类id,email,age和json格式一致publicclassPerson{publicintid{set;get;}publicstringemail{set;get;}publicintage{set;get;}}//这个一个反序列化的方法,用于返回泛型集合publicstaticTJSONToObject<T>(stringjsonText){JavaScriptSerializerjss=newJavaScriptSerializer();returnjss.Deserialize<T>(jsonText);}