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", "未找到相应数据");
                    }


  • 相关阅读:
    浅入浅出---JQuery究竟是什么?
    How far away ?(DFS)
    SoC嵌入式软件架构设计之七:嵌入式文件系统设计
    Java Log Viewer日志查看器
    软件測试基本方法(三)之黑盒測试
    NLP1 —— Python自然语言处理环境搭建
    个性化搜索的几个阶段
    自然语言处理扫盲·第四天——白话人机问答系统原理
    自然语言处理扫盲·第三天——白话情感分析原理
    自然语言处理扫盲·第二天——白话机器翻译原理
  • 原文地址:https://www.cnblogs.com/freexiaoyu/p/2649333.html
Copyright © 2011-2022 走看看