The content of this work is to be the following data analysis. NET can be used, the data returned except header, the other is variable, which means that the structure is not fixed. is completely selected by the user, so the build DataTable is selected.
The JSON data format is as follows:
Copy Code code 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:
Copy Code code as follows:
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>
Returned data structure definition, no
</summary>
Public DataTable Fielddefine
{
get {return fielddefine;}
set {fielddefine = value;}
}
Private DataTable Retrundata = new DataTable ();
<summary>
The data returned is in the same format as the DataTable, structure and Fielddefine
</summary>
Public DataTable Retrundata
{
get {return retrundata;}
set {Retrundata = value;}
}
<summary>
Converts JSON data to a well-defined object and data to a 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>
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 is not a 0 o'clock error message
</summary>
public string ErrorInfo
{
get {return errorinfo;}
set {errorinfo = value;}
}
private int totalrows;
<summary>
Query Results Total Row count
</summary>
public int Totalrows
{
get {return totalrows;}
set {totalrows = value;}
}
private int returnrows;
<summary>
Number of data rows returned
</summary>
public int Returnrows
{
get {return returnrows;}
set {returnrows = value;}
}
}
}
}
How to use:
Generalsearchresult GSR = new Generalsearchresult ();
GSR = Generalsearchresult.gettransformdata (text);