• JSON 序列化格式


    一、C#处理简单json数据
    json数据:

    复制代码代码如下:
    {"result":"0","res_info":"ok","queryorder_info":"info"}


    我这里是以jsonmy1.txt文件的形式保存在d盘json文件夹下。
     
    构造对象:

    复制代码代码如下:

     public struct ToJsonMy
     {
        public string result { get; set; }  //属性的名字,必须与json格式字符串中的"key"值一样。
        public string res_info { get; set; }
        public string queryorder_info { get; set; }   
     }


    转换过程:

    复制代码代码如下:
    public static void JsonMy()
    {
        string json = Jsonstr("D:\json\jsonmy1.txt");//Jsonstr函数读取json数据的文本txt                 
        JavaScriptSerializer js = new JavaScriptSerializer();   //实例化一个能够序列化数据的类
        ToJsonMy list = js.Deserialize<ToJsonMy>(json);    //将json数据转化为对象类型并赋值给list
        string result = list.result;             
        string res_info = list.res_info;
        string queryorder_info = list.res_info;
    }


    二、C#处理包含对象的json数据
    json数据:jsonmy2.txt

    复制代码代码如下:
    {"result":"0","res_info":"ok","queryorder_info":{"order_num":"5","orderdetail":"ok"}}


    构造对象:

    复制代码代码如下:

    public struct ToJsonMy2
    {
        public string result { get; set; }  
        public string res_info { get; set; }
        public queryorder_info queryorder_info;
    }
    public struct queryorder_info
    {
        public string order_num { get; set; }
        public string orderdetail { get; set; }
    };


    转换过程:

    复制代码代码如下:
    public static void JsonMy2()
    {
        string json = Jsonstr("D:\json\jsonmy2.txt");
        JavaScriptSerializer js = new JavaScriptSerializer();   //实例化一个能够序列化数据的类
        ToJsonMy2 list = js.Deserialize<ToJsonMy2>(json);    //将json数据转化为对象类型并赋值给list
        string result = list.result;    
        string res_info = list.res_info;
        string order_num = list.queryorder_info.order_num;
        string orderdetail = list.queryorder_info.orderdetail;
    }


    三、C#处理包含对象、数组的json数据
    json数据:jsonmy4.txt

    复制代码代码如下:
    {"result":"0","res_info":"ok","queryorder_info":{"order_num":"5","orderdetail":[{"CFTUin":"769839263","CancelDeadline":"2013-09-12 23:00:00","CheckInDate":"2013-09-12 00:00:00","CheckOutDate":"2013-09-13 00:00:00","CityID":"0101","CurrencyCode":"RMB","HotelID":"00301105","HotelName":"乐家连锁(北京天坛南门店)(原速8酒店(北京天坛南门店)","ListID":"1000000005201308280002999652","PayAmt":"228","PayType":"0","RommsCnt":"1","SPTransID":"65202157","State":"4"},{"CFTUin":"248486133","CancelDeadline":"2013-10-13 23:00:00","CheckInDate":"2013-10-13 00:00:00","CheckOutDate":"2013-10-18 00:00:00","CityID":"0201","CurrencyCode":"RMB","HotelID":"10201314","HotelName":"上海凯顿酒店","ListID":"1000000005201308280002999413","PayAmt":"1140","PayType":"0","RommsCnt":"1","SPTransID":"65197226","State":"4"}]}}


    构造对象:

    复制代码代码如下:
    public struct ToJsonMy3
    {
        public string result { get; set; } 
        public string res_info { get; set; }
        public queryorder_info queryorder_info;
    }
    public struct queryorder_info
    {
        public string order_num { get; set; }
        public List<orderdetail> orderdetail;//数组处理       
    };
    public struct orderdetail
    {
        public string CFTUin { get; set; }
        public string CancelDeadline { get; set; }
        public string CheckInDate { get; set; }
        public string CheckOutDate { get; set; }
        public string CityID { get; set; }
        public string CurrencyCode { get; set; }
        public string HotelID { get; set; }
        public string HotelName { get; set; }
        public string ListID { get; set; }
        public string PayAmt { get; set; }
        public string PayType { get; set; }
        public string RommsCnt { get; set; }
        public string SPTransID { get; set; }
        public string State { get; set; }
    };

    转换过程:

    复制代码代码如下:
    public static void JsonMy4()
    {
        string json = Jsonstr("D:\json\jsonmy4.txt");
        JavaScriptSerializer js = new JavaScriptSerializer();   //实例化一个能够序列化数据的类
        ToJsonMy3 list = js.Deserialize<ToJsonMy3>(json);    //将json数据转化为对象类型并赋值给list
        string result = list.result;     
        string res_info = list.res_info;
        string order_num = list.queryorder_info.order_num;
        List<orderdetail> orderdetail = list.queryorder_info.orderdetail;
        string CFTUin = orderdetail[0].CFTUin;
        string HotelName = orderdetail[0].HotelName;
        string ListID = orderdetail[1].ListID;
        string State = orderdetail[2].State;
    }

     PS:关于json操作,这里再为大家推荐几款比较实用的json在线工具供大家参考使用:

    在线JSON代码检验、检验、美化、格式化工具:
    http://tools.jb51.net/code/json

    JSON在线格式化工具:
    http://tools.jb51.net/code/jsonformat

    在线XML/JSON互相转换工具:
    http://tools.jb51.net/code/xmljson

    json代码在线格式化/美化/压缩/编辑/转换工具:
    http://tools.jb51.net/code/jsoncodeformat

    在线json压缩/转义工具:

    http://tools.jb51.net/code/json_yasuo_trans

    C语言风格/HTML/CSS/json代码格式化美化工具:
    http://tools.jb51.net/code/ccode_html_css_json

  • 相关阅读:
    想学数据库的进来领课程了哈.....
    天轰穿C# vs2010 04面向对象的编程之密封【原创】
    天轰穿C#vs2010 04面向对象的编程之访问数组 【原创】
    天轰穿C#vs201004面向对象的编程之foreach循环【原创】
    天轰穿C# vs2010 04面向对象的编程之继承【原创】
    【IT职业规划】天轰穿教你如何选择和学习编程
    天轰穿C# vs2010 04面向对象的编程之简单数组 【原创】
    关闭任务计划程序前您必须关闭所有会话框的解决方法
    用树莓派实现会说话的汤姆猫
    备忘录:关于.net程序连接Oracle数据库
  • 原文地址:https://www.cnblogs.com/zhangwei99com/p/7759807.html
Copyright © 2020-2023  润新知