加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Newtonsoft.Json 应用

发布时间:2020-12-16 19:24:02 所属栏目:百科 来源:网络整理
导读: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":tru

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 { ; }

    }
}

序列化JOSN
View Code
 DataTable dt = new Maticsoft.BLL.Iphone_Classify().GetList(parentID=" + strParentID).Tables[0];
                if (dt.Rows.Count > )
                {
                    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 = parentIDbool.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(,JsonConvert.SerializeObject(list));
                }
                
                {
                    Classifyinfo = Json.JsonInfo(400",未找到相应数据);
                }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读