Newtonsoft.Json解析json数据

前端之家收集整理的这篇文章主要介绍了Newtonsoft.Json解析json数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
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':'手机外套',{'_cid':8,'_name':'手机模','_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; }

    }
}
原文链接:https://www.f2er.com/json/290370.html

猜你在找的Json相关文章