asp.net(C#)解析Json的類代碼

來源:互聯網
上載者:User

本次工作內容是要將以下資料解析成.Net可以使用的資料,返回的資料除了header,其他的都是可變的,也就是說結構不是固定的。完全由使用者選擇,所以選擇了產生DataTable。
Json資料格式如下: 複製代碼 代碼如下:{"dataSet":{
"header":{
"returnCode":"0",
"errorInfo":"HTTP請求錯誤",
"version":"V1.0R010",
"totalRows":"2000",
"returnRows":"20"
},
"fieldDefine":{
"assetId":"string",
"serverIdcId":"int",
"inputTime":"datetime"
},
"data":{"row":[
{
"AssetId":"TCNS2006888",
"ServerIdcId":"1",
"InputTime":"2008-12-12"
},
{
"AssetId":"TCNS2006889",
"ServerIdcId":"2",
"InputTime":"2008-1-1"
}
]}
}
}

解析的類: 複製代碼 代碼如下: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));
}
gsr.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; }
}
}
}
}

使用方法:
GeneralSearchResult gsr = new GeneralSearchResult();
gsr = GeneralSearchResult.GetTransformData(text);

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.