C# 解析json。json 序列化
文章来源:http://www.itnose.net/detail/6251947.html
更多文章:http://www.itnose.net
使用C# josn 功能需要添加引用
System.Web.Extensions
1. 对象序列化成json和反序列化
//对象
public class prolist
{
private string _pid = "0";
private string _pmoney = "0";
public string pid
{
set { this._pid = value; }
get { return this._pid; }
}
public string pmoney
{
set { this._pmoney = value; }
get { return this._pmoney; }
}
}
//序列化开始
JavaScriptSerializer sc = new JavaScriptSerializer();
List<prolist> pl = new List<prolist>();
prolist p = new prolist();
p.pid = "1"; p.pmoney = "短信";
pl.Add(p);
prolist p1 = new prolist();
p1.pid = "1"; p1.pmoney = "服务";
pl.Add(p1);
string plstr= sc.Serialize(pl);//对象序列化成josn字符串
//将上边反序列化成对象
List<prolist> pls = new List<prolist>();
pls = sc.Deserialize<List<prolist>>(plstr);
2. 把简单json格式的字符串 解析,不反序列化成对象
//一个对象可以生成键值对 Dictionary<String, String>
string plstr=" {\"proid\":\"1\",\"proname\":\"商品1\"}";
Dictionary<String, String> dic = sc.Deserialize<Dictionary<String, String>>(plstr);
//多组对象解析
string plstr=" [{\"proid\":\"1\",\"proname\":\"商品1\"},{\"proid\":\"1\",\"proname\":\"商品1\"}]";
List<Dictionary<String, String>> lds = sc.Deserialize<List<Dictionary<String, String>>>(plstr);
l