• Newtonsoft.Json用法


    修改http://www.cnblogs.com/freexiaoyu/archive/2012/08/21/2649333.html用法

    还是原来的JSON格式

    Model类

    View Code
    namespace Maticsoft.Model
    {
        /// <summary>
        ///返回JSON属性
        /// </summary>
        [Serializable]
       public class BackInfo<T>
        { 
            /// <summary>
            /// 状态
            /// </summary>
            public String result { get; set; }
            /// <summary>
            /// 返回数据
            /// </summary>
            public List<T> info { get; set; }
        }
    
        /// <summary>
        ///返回JSON属性
        /// </summary>
        [Serializable]
        public class BackDataTable<T>
        {
            /// <summary>
            /// 状态
            /// </summary>
            public String result { get; set; }
            /// <summary>
            /// 返回数据
            /// </summary>
            public T info { get; set; }
        }
    }

    序列化JSON

     Classifyinfo = Json.JsonInfo("200", JsonConvert.SerializeObject(dt)); //这里的dt是datatable

    反序列化JOSN

    View Code
       String JsonData = GetPage("http://localhost:3448/app/api.aspx", "action=ClassifyList&parentID=2");
                Maticsoft.Model.BackDataTable<DataTable> BackInfo = (Maticsoft.Model.BackDataTable<DataTable>)JsonConvert.DeserializeObject(JsonData, typeof(Maticsoft.Model.BackDataTable<DataTable>));
                if (BackInfo.result != "200")
                {
                   //失败
                }
                DataTable dt = BackInfo.info;
                    this.DropDownList3.DataSource = dt;
                    this.DropDownList3.DataTextField = "Name";
                    this.DropDownList3.DataValueField = "CID";
                    this.DropDownList3.DataBind(); 
    
                Maticsoft.Model.BackDataTable<List<Model.Iphone_Classify>> BackInfo1 =
                    (Maticsoft.Model.BackDataTable<List<Model.Iphone_Classify>>)
                    JsonConvert.DeserializeObject(JsonData, typeof(Maticsoft.Model.BackDataTable<List<Model.Iphone_Classify>>));
    
                if (BackInfo1.result != "200")
                {
                    //失败 
                }
                /*List<Model.Iphone_Classify> classify = BackInfo1.info;
                   this.DropDownList3.DataSource = classify;
                   this.DropDownList3.DataTextField = "Name";
                   this.DropDownList3.DataValueField = "CID";
                   this.DropDownList3.DataBind();*/
    
    
                Maticsoft.Model.BackInfo<Model.Iphone_Classify> BackInfo2 = (Maticsoft.Model.BackInfo<Model.Iphone_Classify>)
                    JsonConvert.DeserializeObject(JsonData, typeof(Maticsoft.Model.BackInfo<Model.Iphone_Classify>));
                if (BackInfo2.result != "200")
                {//失败 
                }
                /*List<Model.Iphone_Classify> classify = BackInfo2.info;
                   this.DropDownList3.DataSource = classify;
                   this.DropDownList3.DataTextField = "Name";
                   this.DropDownList3.DataValueField = "CID";
                   this.DropDownList3.DataBind();*/

    如果序列化JSON直接用的是datatable的话反序列化直接用

    DataTable dt=(DataTable)JsonConvert.DeserializeObject<DataTable>(JsonData);

  • 相关阅读:
    conrtex 和 ARM 的关系
    C语言中的内存分配深入
    钳位电路
    向量中断与非向量中断的区别
    Freescale PowerPC处理器优势
    路由器端口映射,远程桌面连接端口映射+花生壳=让人访问你个人服务器或WEB站点
    linux 下用G++编译C++
    Javascript基础知识篇(1): 初识Javascript
    Javascript实战应用篇(3):动态加载JS和CSS文件
    Javascript高级技术篇(1):搭建JS框架类库
  • 原文地址:https://www.cnblogs.com/freexiaoyu/p/2738974.html
Copyright © 2020-2023  润新知