asp.net(C#)解析Json的類代碼
更新時間:2009年12月22日 22:19:53 作者:
現(xiàn)在json因為輕型,越來越流行,部門內部的數(shù)據(jù)標準趨向于json,所以開始學習。
本次工作內容是要將以下數(shù)據(jù)解析成.Net可以使用的數(shù)據(jù),返回的數(shù)據(jù)除了header,其他的都是可變的,也就是說結構不是固定的。完全由用戶選擇,所以選擇了生成DataTable。
Json數(shù)據(jù)格式如下:
{"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>
/// 返回的數(shù)據(jù)結構定義,無數(shù)據(jù)
/// </summary>
public DataTable FieldDefine
{
get { return fieldDefine; }
set { fieldDefine = value; }
}
private DataTable retrunData = new DataTable();
/// <summary>
/// 返回的數(shù)據(jù),格式為DataTable,結構和FieldDefine中的結構一樣
/// </summary>
public DataTable RetrunData
{
get { return retrunData; }
set { retrunData = value; }
}
/// <summary>
/// 將json數(shù)據(jù)轉換為定義好的對象,數(shù)據(jù)轉換為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>
/// 數(shù)據(jù)文件頭定義
/// </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>
/// 查詢結果總行數(shù)
/// </summary>
public int TotalRows
{
get { return totalRows; }
set { totalRows = value; }
}
private int returnRows;
/// <summary>
/// 返回的數(shù)據(jù)行數(shù)
/// </summary>
public int ReturnRows
{
get { return returnRows; }
set { returnRows = value; }
}
}
}
}
使用方法:
GeneralSearchResult gsr = new GeneralSearchResult();
gsr = GeneralSearchResult.GetTransformData(text);
Json數(shù)據(jù)格式如下:
復制代碼 代碼如下:
{"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>
/// 返回的數(shù)據(jù)結構定義,無數(shù)據(jù)
/// </summary>
public DataTable FieldDefine
{
get { return fieldDefine; }
set { fieldDefine = value; }
}
private DataTable retrunData = new DataTable();
/// <summary>
/// 返回的數(shù)據(jù),格式為DataTable,結構和FieldDefine中的結構一樣
/// </summary>
public DataTable RetrunData
{
get { return retrunData; }
set { retrunData = value; }
}
/// <summary>
/// 將json數(shù)據(jù)轉換為定義好的對象,數(shù)據(jù)轉換為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>
/// 數(shù)據(jù)文件頭定義
/// </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>
/// 查詢結果總行數(shù)
/// </summary>
public int TotalRows
{
get { return totalRows; }
set { totalRows = value; }
}
private int returnRows;
/// <summary>
/// 返回的數(shù)據(jù)行數(shù)
/// </summary>
public int ReturnRows
{
get { return returnRows; }
set { returnRows = value; }
}
}
}
}
使用方法:
GeneralSearchResult gsr = new GeneralSearchResult();
gsr = GeneralSearchResult.GetTransformData(text);
相關文章
ASP.NET Core如何添加統(tǒng)一模型驗證處理機制詳解
這篇文章主要給大家介紹了關于ASP.NET Core如何添加統(tǒng)一模型驗證處理機制的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或者使用ASP.NET Core具有一定的參考學習價值,需要的朋友可以參考下2018-05-05利用noesis.Javascript開源組件.Net中執(zhí)行javascript腳本
利用Noesis.Javascript開源組件可以做到在.net中執(zhí)行js腳本,同時js腳本也能調用C#函數(shù)。這個組件的獲得方式:在NuGet中輸入搜索"Noesis"就能找到,我們來做個搜索功能:用戶能夠在textbox中輸入js腳本來篩選list記錄2013-12-12在 .NET Core 中使用 Diagnostics (Diagnostic Source) 記錄跟蹤信息
今天給大家講一下在 .NET Core 2 中引入的全新 DiagnosticSource 事件機制,為什么說是全新呢? 在以前的 .NET Framework 有心的同學應該知道也有 Diagnostics,那么新的 .NET Core 中有什么變化呢?跟隨小編一起看看吧2021-06-06ASP.NET中GridView和Repeater重復數(shù)據(jù)如何合并
這篇文章主要介紹了ASP.NET中GridView和Repeater重復數(shù)據(jù)合并的方法,感興趣的小伙伴們可以參考一下2016-08-08asp.net GridView控件中實現(xiàn)全選的解決方案
在GridView中我們經常要利用復選按鈕實現(xiàn)全選的功能,下面針對這一解決方案做以總結2010-03-03mstest實現(xiàn)類似單元測試nunit中assert.throws功能
我們做單元測試NUnit中,有一個斷言Assert.Throws很好用,現(xiàn)在我們來擴展一下也實現(xiàn)類似成功能,大家參考使用吧2014-01-01