asp.Net(C#)解析Jsonのクラスコード
5427 ワード
今回の作業内容は、以下のデータを解析する.Netで使用可能なデータは,返されるデータはヘッダを除いて可変,すなわち構造が固定されていない.完全にユーザによって選択されるので、DataTableの生成を選択した.
Jsonデータフォーマットは次のとおりです.
解析されたクラス:
使用方法:
GeneralSearchResult gsr = new GeneralSearchResult();
gsr = GeneralSearchResult.GetTransformData(text);
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();
///
/// ,
///
public DataTable FieldDefine
{
get { return fieldDefine; }
set { fieldDefine = value; }
}
private DataTable retrunData = new DataTable();
///
/// , DataTable, FieldDefine
///
public DataTable RetrunData
{
get { return retrunData; }
set { retrunData = value; }
}
///
/// json , DataTable
///
///
///
public static GeneralSearchResult GetTransformData(string jsonText)
{
GeneralSearchResult gsr = new GeneralSearchResult();
JavaScriptSerializer s = new JavaScriptSerializer();
Dictionary JsonData = (Dictionary)s.DeserializeObject(jsonText);
Dictionary dataSet = (Dictionary)JsonData["dataSet"];
Dictionary header = (Dictionary)dataSet["header"];
Dictionary fieldDefine = (Dictionary)dataSet["header"];
Dictionary data = (Dictionary)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 dicFieldDefine = (Dictionary)dataSet["fieldDefine"];
foreach (KeyValuePair ss in dicFieldDefine)
{
gsr.FieldDefine.Columns.Add(ss.Key, typeof(string));
}
gsr.RetrunData = gsr.FieldDefine.Clone();
foreach (object ob in rows)
{
Dictionary val = (Dictionary)ob;
DataRow dr = gsr.RetrunData.NewRow();
foreach (KeyValuePair sss in val)
{
dr[sss.Key] = sss.Value;
}
gsr.RetrunData.Rows.Add(dr);
}
return gsr;
}
///
///
///
public class Header
{
private string version;
///
///
///
public string Version
{
get { return version; }
set { version = value; }
}
private string returnCode;
///
/// ,0 ,
///
public string ReturnCode
{
get { return returnCode; }
set { returnCode = value; }
}
private string errorInfo;
///
/// ReturnCode 0
///
public string ErrorInfo
{
get { return errorInfo; }
set { errorInfo = value; }
}
private int totalRows;
///
///
///
public int TotalRows
{
get { return totalRows; }
set { totalRows = value; }
}
private int returnRows;
///
///
///
public int ReturnRows
{
get { return returnRows; }
set { returnRows = value; }
}
}
}
}
使用方法:
GeneralSearchResult gsr = new GeneralSearchResult();
gsr = GeneralSearchResult.GetTransformData(text);