C # JSON data is deserialized into dictionary and gets the specified value based on the keyword

Source: Internet
Author: User

JSON data:

{
"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"
}
]
}
}
}

Question: How do I get the rows of data in the header so that they appear on the interface?

To convert the JSON data into dictionary code:

<summary>
Deserializing JSON data into dictionary
</summary>
<param name= "Jsondata" >json data </param>
<returns></returns>
Private dictionary<string, object> jsontodictionary (string jsondata)
{
Instantiate a new instance of the JavaScriptSerializer class
JavaScriptSerializer JSS = new JavaScriptSerializer ();
Try
{
Converts the specified JSON string to dictionary<string, object> object of type
Return JSS. Deserialize<dictionary<string, object>> (Jsondata);
}
catch (Exception ex)
{
throw new Exception (ex. Message);
}
}

How to use:

private void Button1_Click (object sender, EventArgs e)
{
JSON data
String json = "{\" 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\ "}]}}";
dictionary<string, object> dic = Jsontodictionary (JSON);//convert JSON data to Dictionary format
Dictionary<string, object> dataset= (dictionary<string, object>) dic["DataSet"];
Traversing data using KeyValuePair
foreach (keyvaluepair<string, object> item in DataSet)
{
if (item. key.tostring () = = "header")//Get Header data
{
var subitem= (dictionary<string,object>) item. Value;
foreach (Var str in subitem)
{
Textbox1.appendtext (str. Key + ":" + str. Value+ "\ r \ n");//Display to interface
}
Break
}
}
}

C # JSON data is deserialized into dictionary and gets the specified value based on the keyword

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.