zoukankan      html  css  js  c++  java
  • 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", "未找到相应数据");
                    }


  • 相关阅读:
    apache安全—用户访问控制
    hdu 3232 Crossing Rivers 过河(数学期望)
    HDU 5418 Victor and World (可重复走的TSP问题,状压dp)
    UVA 11020 Efficient Solutions (BST,Splay树)
    UVA 11922 Permutation Transformer (Splay树)
    HYSBZ 1208 宠物收养所 (Splay树)
    HYSBZ 1503 郁闷的出纳员 (Splay树)
    HDU 5416 CRB and Tree (技巧)
    HDU 5414 CRB and String (字符串,模拟)
    HDU 5410 CRB and His Birthday (01背包,完全背包,混合)
  • 原文地址:https://www.cnblogs.com/freexiaoyu/p/2649333.html
Copyright © 2011-2022 走看看