Asp.net (C #) parses Json class code

Source: Internet
Author: User

This article resolves the following data into data that can be used by. Net. The returned data except the header is variable, that is, the structure is not fixed. The generated able is selected by the user.
The Json data format is as follows:
Copy codeThe Code 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": "maid"
},
{
"AssetId": "TCNS2006889 ",
"ServerIdcId": "2 ",
"InputTime": "maid"
}
]}
}
}

Parsed class:
Copy codeThe Code is 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 data
/// </Summary>
Public DataTable FieldDefine
{
Get {return fieldDefine ;}
Set {fieldDefine = value ;}
}
Private DataTable retrunData = new DataTable ();
/// <Summary>
/// The returned data is in the format of able. The structure is the same as that in FieldDefine.
/// </Summary>
Public DataTable RetrunData
{
Get {return retrunData ;}
Set {retrunData = value ;}
}
/// <Summary>
/// Convert json data into defined objects and convert data into 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. If the value 0 is normal, otherwise an error occurs.
/// </Summary>
Public string ReturnCode
{
Get {return returnCode ;}
Set {returnCode = value ;}
}
Private string errorInfo;
/// <Summary>
/// Error message if ReturnCode is not 0
/// </Summary>
Public string ErrorInfo
{
Get {return errorInfo ;}
Set {errorInfo = value ;}
}
Private int totalRows;
/// <Summary>
/// Total number of rows in the query result
/// </Summary>
Public int TotalRows
{
Get {return totalRows ;}
Set {totalRows = value ;}
}
Private int returnRows;
/// <Summary>
/// Number of returned data rows
/// </Summary>
Public int ReturnRows
{
Get {return returnRows ;}
Set {returnRows = value ;}
}
}
}
}

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.