Newtonsoft.Json下载地址http://json.codeplex.com/
根据自己的项目生成DLL并引用
JSON格式为
{"result":"200","info":[{"_cid":5,"_name":"Iphone4","_parentid":2,"_status":true},{"_cid":7,"_name":"手机外套",{"_cid":8,"_name":"手机模","_status":true}]} 解析JOSN
View Code
Maticsoft.Model.BackInfo backinfo = (Maticsoft.Model.BackInfo) JsonConvert.DeserializeObject(JsonData,typeof(Maticsoft.Model.BackInfo)); if (backinfo.result == "200")//成功 { List<Maticsoft.Model.IphoneClassify> list = backinfo.info; Dictionary<int,String> dic = new System.Collections.Generic.Dictionary<(); for (int i = 0; i < list.Count; i++) { Maticsoft.Model.IphoneClassify classify = (Maticsoft.Model.IphoneClassify)list[i]; dic.Add(classify._cid,classify._name); } this.DataList1.DataSource = dic; this.DataList1.DataBind(); } else { 失败 }
这里特别注意的是 JSON的字段要和Model类的属性名保持一至不然返回Null 就是这个问题研究半天才弄明白,多谢http://smalltalk.cnblogs.com/和http://www.cnblogs.com/xiaopohou/
的帮忙
我的model类是用动软生成 的
最后将model类改为
View Code
using System; System.Collections.Generic; System.Linq; System.Text; namespace Maticsoft.Model { /// <summary> ///返回JSON属性 </summary> [Serializable] public class BackInfo { public BackInfo() { } public String result { get; set; } public List<IphoneClassify> info { ; } } 产品类型 [Serializable] IphoneClassify { int _cid { string _name { int _parentid { bool _status { ; } } }