Newtonsoft.Json 应用

Newtonsoft.Json下载地址http://json.codeplex.com/

根据自己的项目生成DLL并引用

JSON格式为

{"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}]}

解析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<int, String>();
                 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;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Maticsoft.Model
{
    /// <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; }

    }
}
复制代码

 

序列化JOSN
View Code
复制代码
 DataTable dt = new Maticsoft.BLL.Iphone_Classify().GetList("parentID=" + strParentID).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    List<Maticsoft.Model.Iphone_Classify> list = new List<Model.Iphone_Classify>();
                    foreach (DataRow row in dt.Rows)
                    {
                        Maticsoft.Model.Iphone_Classify model = new Model.Iphone_Classify()
                        {
                            CID = int.Parse(row["CID"].ToString()),
                            Name = row["Name"].ToString(),
                            parentID = int.Parse(row["parentID"].ToString()),
                            Status = bool.Parse(row["Status"].ToString())
                        };
                        list.Add(model);
                    }
                    //微软的JSON序列化
                   // Classifyinfo = Json.JsonInfo("200", Json.CompanyJsonSerializer<List<Maticsoft.Model.Iphone_Classify>>(list));
                    // Newtonsoft.Json
                    Classifyinfo = Json.JsonInfo("200", JsonConvert.SerializeObject(list));
                }
                else
                {
                    Classifyinfo = Json.JsonInfo("400", "未找到相应数据");
                }
复制代码

 


你可能感兴趣的:(Newtonsoft.Json 应用)