探索非同凡响的Json数据格式说明

开发
这样,在Response.Write(value.ToString())就会显示paulleder,成功获得json的值了。这里主要用到Json数据格式。

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

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

Json数据格式如下:

using System;  
 
using System.Collections.Generic;  
 
using System.Text;  
 
using System.Data;  
 
using System.Web.Script.Serialization;  
 
 
 
namespace Tencent.Itil.Cmsi.Common  
 
{  
 
     public class GeneralSearchResult  
 
     {  
 
         public Header header = new Header();  
 
         private DataTable fieldDefine = new DataTable();  
 
         /// <summary> 
 
         /// 返回的数据结构定义,无数据  
 
         /// </summary> 
 
         public DataTable FieldDefine  
 
         {  
 
             get { return fieldDefine; }  
 
             set { fieldDefine = value; }  
 
         }  
 
 
 
         private DataTable retrunData = new DataTable();  
 
         /// <summary> 
 
         /// 返回的数据,格式为DataTable,结构和FieldDefine中的结构一样  
 
         /// </summary> 
 
         public DataTable RetrunData  
 
         {  
 
             get { return retrunData; }  
 
             set { retrunData = value; }  
 
         }  
 
 
 
         /// <summary> 
 
         /// 将json数据转换为定义好的对象,数据转换为DataTable  
 
         /// </summary> 
 
         /// <param name="jsonText"></param> 
 
         /// <returns></returns> 
 
         public static GeneralSearchResult GetTransformData(string jsonText)  
 
         {  
 
             GeneralSearchResult gsr = new GeneralSearchResult();  
 
 
 
             JavaScriptSerializer s = new JavaScriptSerializer();  
 
             Dictionary<string, object> JsonData = (Dictionary<string, object>)s.DeserializeObject(jsonText);  
 
             Dictionary<string, object> dataSet = (Dictionary<string, object>)JsonData["dataSet"];  
 
             Dictionary<string, object> header = (Dictionary<string, object>)dataSet["header"];  
 
             Dictionary<string, object> fieldDefine = (Dictionary<string, object>)dataSet["header"];  
 
             Dictionary<string, object> data = (Dictionary<string, object>)dataSet["data"];  
 
             object[] rows = (object[])data["row"];  
 
             gsr.header.Version = header["version"].ToString();  
 
             gsr.header.ErrorInfo = header["errorInfo"].ToString();  
 
             gsr.header.ReturnCode = header["returnCode"].ToString();  
 
             gsr.header.ReturnRows = Convert.ToInt16(header["returnRows"]);  
 
             gsr.header.TotalRows = Convert.ToInt16(header["totalRows"]);  
 
 
 
             Dictionary<string, object> dicFieldDefine = (Dictionary<string, object>)dataSet["fieldDefine"];  
 
             foreach (KeyValuePair<string, object> ss in dicFieldDefine)  
 
             {  
 
 
 
                 gsr.FieldDefine.Columns.Add(ss.Key, typeof(string));  
 
 
 
             }  
 
             gsrgsr.RetrunData = gsr.FieldDefine.Clone();  
 
             foreach (object ob in rows)  
 
             {  
 
                 Dictionary<string, object> val = (Dictionary<string, object>)ob;  
 
                 DataRow dr = gsr.RetrunData.NewRow();  
 
                 foreach (KeyValuePair<string, object> sss in val)  
 
                 {  
 
                     dr[sss.Key] = sss.Value;  
 
                 }  
 
                 gsr.RetrunData.Rows.Add(dr);  
 
             }  
 
             return gsr;  
 
         }  
 
         /// <summary> 
 
         /// 数据文件头定义  
 
         /// </summary> 
 
         public class Header  
 
         {  
 
             private string version;  
 
             /// <summary> 
 
             /// 版本  
 
             /// </summary> 
 
             public string Version  
 
             {  
 
                 get { return version; }  
 
                 set { version = value; }  
 
             }  
 
             private string returnCode;  
 
             /// <summary> 
 
             /// 结果码,0为正常,否则为有错误  
 
             /// </summary> 
 
             public string ReturnCode  
 
             {  
 
                 get { return returnCode; }  
 
                 set { returnCode = value; }  
 
             }  
 
             private string errorInfo;  
 
             /// <summary> 
 
             /// 如果ReturnCode为非0时的错误信息  
 
             /// </summary> 
 
             public string ErrorInfo  
 
             {  
 
                 get { return errorInfo; }  
 
                 set { errorInfo = value; }  
 
             }  
 
             private int totalRows;  
 
             /// <summary> 
 
             /// 查询结果总行数  
 
             /// </summary> 
 
             public int TotalRows  
 
             {  
 
                 get { return totalRows; }  
 
                 set { totalRows = value; }  
 
             }  
 
             private int returnRows;  
 
             /// <summary> 
 
             /// 返回的数据行数  
 
             /// </summary> 
 
             public int ReturnRows  
 
             {  
 
                 get { return returnRows; }  
 
                 set { returnRows = value; }  
 
             }  
 
         }  
 
     }  
 

  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.
  • 247.
  • 248.
  • 249.
  • 250.
  • 251.
责任编辑:chenqingxiang 来源: 互联网
相关推荐

2014-08-12 10:15:42

数据格式JSONXML

2009-09-07 19:02:07

JSON是什么

2013-03-27 10:51:44

iOSjson解析网络交互数据格式解析

2011-04-11 09:48:59

AjaxWEB服务

2024-04-15 13:13:04

PythonJSON

2018-09-25 15:28:57

维谛技术

2010-01-06 13:23:20

JSON数据格式

2024-11-12 12:08:06

JSON数据技巧

2010-01-06 15:03:34

JSON格式封装

2011-12-02 10:34:54

Win7

2010-01-07 17:48:02

JSON结构

2010-01-06 17:06:05

Json格式

2010-01-05 17:35:09

JSON数组格式

2021-11-11 23:16:33

前端数据格式Web

2010-01-08 15:37:59

JSON数据

2011-04-11 13:14:58

AjaxWEB服务

2010-07-09 10:42:38

HART协议

2024-12-19 00:12:02

APIJSON数据

2010-07-09 10:27:33

SQL Server数

2022-06-05 14:57:35

发送钉钉运维架构
点赞
收藏

51CTO技术栈公众号