JSon數據格式在日常工作中還是非常實用的,只需要Json數據就可以了,如果對JSon數據不太了解,那就必須先要對下面就對 進行學習,下面就對JSon數據格式的代碼進行系統的分析與研究。。
看到另一篇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; }
- }
- }
- }
- }