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