探索非同凡响的Json数据格式说明
创始人
2024-06-19 14:21:31
0

Json数据格式在日常工作中还是非常实用的,只需要Json数据就可以了,如果对Json数据不太了解,那就必须先要对下面就对  进行学习,下面就对Json数据格式的代码进行系统的分析与研究。。#t#

看到另一篇C#解析Json的类 的文章现在json因为轻型,越来越流行,部门内部的数据标准趋向于json,所以开始学习。本次工作内容是要将以下数据解析成.Net可以使用的数据,返回的数据除了header,其他的都是可变的,也就是说结构不是固定的。完全由用户选择,所以选择了生成DataTable。

Json数据格式如下:

  1. using System;  
  2.  
  3. using System.Collections.Generic;  
  4.  
  5. using System.Text;  
  6.  
  7. using System.Data;  
  8.  
  9. using System.Web.Script.Serialization;  
  10.  
  11.  
  12.  
  13. namespace Tencent.Itil.Cmsi.Common  
  14.  
  15. {  
  16.  
  17.      public class GeneralSearchResult  
  18.  
  19.      {  
  20.  
  21.          public Header header = new Header();  
  22.  
  23.          private DataTable fieldDefine = new DataTable();  
  24.  
  25.          ///  
  26.  
  27.          /// 返回的数据结构定义,无数据  
  28.  
  29.          /// 
  30.  
  31.  
  32.          public DataTable FieldDefine  
  33.  
  34.          {  
  35.  
  36.              get { return fieldDefine; }  
  37.  
  38.              set { fieldDefine = value; }  
  39.  
  40.          }  
  41.  
  42.  
  43.  
  44.          private DataTable retrunData = new DataTable();  
  45.  
  46.          ///  
  47.  
  48.          /// 返回的数据,格式为DataTable,结构和FieldDefine中的结构一样  
  49.  
  50.          /// 
  51.  
  52.  
  53.          public DataTable RetrunData  
  54.  
  55.          {  
  56.  
  57.              get { return retrunData; }  
  58.  
  59.              set { retrunData = value; }  
  60.  
  61.          }  
  62.  
  63.  
  64.  
  65.          ///  
  66.  
  67.          /// 将json数据转换为定义好的对象,数据转换为DataTable  
  68.  
  69.          /// 
  70.  
  71.  
  72.          ///  name="jsonText"> 
  73.  
  74.          ///  
  75.  
  76.          public static GeneralSearchResult GetTransformData(string jsonText)  
  77.  
  78.          {  
  79.  
  80.              GeneralSearchResult gsr = new GeneralSearchResult();  
  81.  
  82.  
  83.  
  84.              JavaScriptSerializer s = new JavaScriptSerializer();  
  85.  
  86.              Dictionary, object> JsonData = (Dictionary, object>)s.DeserializeObject(jsonText);  
  87.  
  88.              Dictionary, object> dataSet = (Dictionary, object>)JsonData["dataSet"];  
  89.  
  90.              Dictionary, object> header = (Dictionary, object>)dataSet["header"];  
  91.  
  92.              Dictionary, object> fieldDefine = (Dictionary, object>)dataSet["header"];  
  93.  
  94.              Dictionary, object> data = (Dictionary, object>)dataSet["data"];  
  95.  
  96.              object[] rows = (object[])data["row"];  
  97.  
  98.              gsr.header.Version = header["version"].ToString();  
  99.  
  100.              gsr.header.ErrorInfo = header["errorInfo"].ToString();  
  101.  
  102.              gsr.header.ReturnCode = header["returnCode"].ToString();  
  103.  
  104.              gsr.header.ReturnRows = Convert.ToInt16(header["returnRows"]);  
  105.  
  106.              gsr.header.TotalRows = Convert.ToInt16(header["totalRows"]);  
  107.  
  108.  
  109.  
  110.              Dictionary, object> dicFieldDefine = (Dictionary, object>)dataSet["fieldDefine"];  
  111.  
  112.              foreach (KeyValuePair, object> ss in dicFieldDefine)  
  113.  
  114.              {  
  115.  
  116.  
  117.  
  118.                  gsr.FieldDefine.Columns.Add(ss.Key, typeof(string));  
  119.  
  120.  
  121.  
  122.              }  
  123.  
  124.              gsrgsr.RetrunData = gsr.FieldDefine.Clone();  
  125.  
  126.              foreach (object ob in rows)  
  127.  
  128.              {  
  129.  
  130.                  Dictionary, object> val = (Dictionary, object>)ob;  
  131.  
  132.                  DataRow dr = gsr.RetrunData.NewRow();  
  133.  
  134.                  foreach (KeyValuePair, object> sss in val)  
  135.  
  136.                  {  
  137.  
  138.                      dr[sss.Key] = sss.Value;  
  139.  
  140.                  }  
  141.  
  142.                  gsr.RetrunData.Rows.Add(dr);  
  143.  
  144.              }  
  145.  
  146.              return gsr;  
  147.  
  148.          }  
  149.  
  150.          ///  
  151.  
  152.          /// 数据文件头定义  
  153.  
  154.          /// 
  155.  
  156.  
  157.          public class Header  
  158.  
  159.          {  
  160.  
  161.              private string version;  
  162.  
  163.              ///  
  164.  
  165.              /// 版本  
  166.  
  167.              /// 
  168.  
  169.  
  170.              public string Version  
  171.  
  172.              {  
  173.  
  174.                  get { return version; }  
  175.  
  176.                  set { version = value; }  
  177.  
  178.              }  
  179.  
  180.              private string returnCode;  
  181.  
  182.              ///  
  183.  
  184.              /// 结果码,0为正常,否则为有错误  
  185.  
  186.              /// 
  187.  
  188.  
  189.              public string ReturnCode  
  190.  
  191.              {  
  192.  
  193.                  get { return returnCode; }  
  194.  
  195.                  set { returnCode = value; }  
  196.  
  197.              }  
  198.  
  199.              private string errorInfo;  
  200.  
  201.              ///  
  202.  
  203.              /// 如果ReturnCode为非0时的错误信息  
  204.  
  205.              /// 
  206.  
  207.  
  208.              public string ErrorInfo  
  209.  
  210.              {  
  211.  
  212.                  get { return errorInfo; }  
  213.  
  214.                  set { errorInfo = value; }  
  215.  
  216.              }  
  217.  
  218.              private int totalRows;  
  219.  
  220.              ///  
  221.  
  222.              /// 查询结果总行数  
  223.  
  224.              /// 
  225.  
  226.  
  227.              public int TotalRows  
  228.  
  229.              {  
  230.  
  231.                  get { return totalRows; }  
  232.  
  233.                  set { totalRows = value; }  
  234.  
  235.              }  
  236.  
  237.              private int returnRows;  
  238.  
  239.              ///  
  240.  
  241.              /// 返回的数据行数  
  242.  
  243.              /// 
  244.  
  245.  
  246.              public int ReturnRows  
  247.  
  248.              {  
  249.  
  250.                  get { return returnRows; }  
  251.  
  252.                  set { returnRows = value; }  
  253.  
  254.              }  
  255.  
  256.          }  
  257.  
  258.      }  
  259.  

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
Windows恶意软件20年“... 在Windows的早期年代,病毒游走于系统之间,偶尔删除文件(但被删除的文件几乎都是可恢复的),并弹...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
着眼MAC地址,解救无法享受D... 在安装了DHCP服务器的局域网环境中,每一台工作站在上网之前,都要先从DHCP服务器那里享受到地址动...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...