当前位置: 代码迷 >> 综合 >> Newtonsoft.Json解析json数据
  详细解决方案

Newtonsoft.Json解析json数据

热度:88   发布时间:2023-12-21 08:25:03.0
namespace WindowsFormsApplication2
{public partial class Form1 : Form{public Form1(){InitializeComponent();string JsonData="{'result':'200','info':[{'_cid':5,'_name':'Iphone4','_parentid':2,'_status':true},{'_cid':7,'_name':'手机外套','_parentid':2,'_status':true},{'_cid':8,'_name':'手机模','_parentid':2,'_status':true}]}";WindowsFormsApplication2.BackInfo backinfo = (WindowsFormsApplication2.BackInfo)JsonConvert.DeserializeObject(JsonData, typeof(WindowsFormsApplication2.BackInfo));if (backinfo.result == "200")//成功{List<WindowsFormsApplication2.IphoneClassify> list = backinfo.info;Dictionary<int, String> dic = new System.Collections.Generic.Dictionary<int, String>();for (int i = 0; i < list.Count; i++){WindowsFormsApplication2.IphoneClassify classify = (WindowsFormsApplication2.IphoneClassify)list[i];dic.Add(classify._cid, classify._name);}//this.DataList1.DataSource = dic;//this.DataList1.DataBind();}else{//失败}}}/// <summary>///返回JSON属性/// </summary>[Serializable]public class BackInfo{public BackInfo(){}public String result { get; set; }public List<IphoneClassify> info { get; set; }}/// <summary>///产品类型/// </summary>[Serializable]public class IphoneClassify{public int _cid { get; set; }public string _name { get; set; }public int _parentid { get; set; }public bool _status { get; set; }}
}

  相关解决方案