asp.net of C parses Json's class code

  • 2020-05-07 19:28:09
  • OfStack

This work is to parse the following data into data that can be used by.Net. The returned data is all variable except header, that is to say, the structure is not fixed. It was entirely up to the user to choose, so DataTable was selected.
Json data format is as follows:
 
 { "dataSet":{ 
"header":{ 
"returnCode":"0", 
"errorInfo":"HTTP Request error ", 
"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" 
} 
]} 
} 
} 

Resolved classes:
 
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> 
///  Data structure definition returned, no data  
/// </summary> 
public DataTable FieldDefine 
{ 
get { return fieldDefine; } 
set { fieldDefine = value; } 
} 
private DataTable retrunData = new DataTable(); 
/// <summary> 
///  The data returned is in the format of DataTable , the structure and FieldDefine In the structure 1 sample  
/// </summary> 
public DataTable RetrunData 
{ 
get { return retrunData; } 
set { retrunData = value; } 
} 
/// <summary> 
///  will json Data is converted to a defined object, and data is converted to 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> 
///  Data file header definition  
/// </summary> 
public class Header 
{ 
private string version; 
/// <summary> 
///  version  
/// </summary> 
public string Version 
{ 
get { return version; } 
set { version = value; } 
} 
private string returnCode; 
/// <summary> 
///  The result code, 0 Is normal, otherwise there is an error  
/// </summary> 
public string ReturnCode 
{ 
get { return returnCode; } 
set { returnCode = value; } 
} 
private string errorInfo; 
/// <summary> 
///  if ReturnCode For the 0 Error message when  
/// </summary> 
public string ErrorInfo 
{ 
get { return errorInfo; } 
set { errorInfo = value; } 
} 
private int totalRows; 
/// <summary> 
///  Total number of rows of query results  
/// </summary> 
public int TotalRows 
{ 
get { return totalRows; } 
set { totalRows = value; } 
} 
private int returnRows; 
/// <summary> 
///  Number of rows of data returned  
/// </summary> 
public int ReturnRows 
{ 
get { return returnRows; } 
set { returnRows = value; } 
} 
} 
} 
} 

Usage:
GeneralSearchResult gsr = new GeneralSearchResult();
gsr = GeneralSearchResult.GetTransformData(text);

Related articles: