欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Json数据转换list对象实现思路及代码

程序员文章站 2024-03-03 19:47:04
json代码: 复制代码 代码如下: [{"cid":2,"cname":"\u767d\u841d\u535c","amount":26,"price":"17"},{"...
json代码
复制代码 代码如下:

[{"cid":2,"cname":"\u767d\u841d\u535c","amount":26,"price":"17"},{"cid":3,"cname":"\u80e1\u841d\u535c","amount":21,"price":"21"},{"cid":4,"cname":"\u7389\u7c73","amount":7,"price":"23"},{"cid":14,"cname":"\u897f\u74dc","amount":7,"price":"29"},{"cid":5,"cname":"\u571f\u8c46","amount":3,"price":"24"},{"cid":1,"cname":"\u8349\u8393","amount":427,"price":"27"},{"cid":10,"cname":"\u5357\u74dc","amount":303,"price":"30"},{"cid":11,"cname":"\u82f9\u679c","amount":1,"price":"24"},{"cid":23,"cname":"\u77f3\u69b4","amount":4,"price":"54"},{"cid":18,"cname":"\u6843\u5b50","amount":3,"price":"40"},{"cid":15,"cname":"\u9999\u8549","amount":9,"price":"32"},{"cid":13,"cname":"\u8461\u8404","amount":7,"price":"47"},{"cid":26,"cname":"\u67da\u5b50","amount":1,"price":"58"},{"cid":27,"cname":"\u83e0\u841d","amount":5,"price":"62"},{"cid":19,"cname":"\u6a59\u5b50","amount":27,"price":"41"}]

vs2008
引用system.servicemodel.web.dll
引用system.runtime.serialization.dll
复制代码 代码如下:

private void form1_load(object sender, eventargs e)
{
string _jsontext = system.io.file.readalltext(@"c:\2.txt");
list<test> _test = new list<test>();
datacontractjsonserializer _json = new datacontractjsonserializer(_test.gettype());
byte[] _using = system.text.encoding.utf8.getbytes(_jsontext);
system.io.memorystream _memorystream = new system.io.memorystream(_using);
_memorystream.position = 0;
_test =(list<test>)_json.readobject(_memorystream);
}
public class test
{
public int cid = 0;
public string cname = "";
public int amount = 0;
public string price = "";
}