This article mainly introduces the method of extracting multilayer nested JSON data from ASP, using the third party class library Newtonsoft.json to extract multilayer nested JSON data, which is interesting to understand.
In this paper, we describe the method of extracting multiple layers of nested JSON data using the third-party class library Newtonsoft.json, in the following examples.
Suppose you want to extract the JSON string as follows:
{"Name": "Lily", "Age": $, "addr": {"City": Guangzhou, "Province": Guangdong}}
To refer to a namespace first:
Using newtonsoft.json;using Newtonsoft.Json.Linq;
You can think of the JSON string above as an object, as long as you write the corresponding class (if you are developing using VS2013, you can quickly convert the JSON string to an entity class by "edit-paste Paste as a class")
public class UserInfo {public string name; public int age; public address addr; public class Address {public string city; public string Province; }
1. The code to convert the JSON string to an entity object is as follows:
String Jsondata= "{\" name\ ": \" lily\ ", \" age\ ": 23,\" addr\ ": {\" city\ ": Guangzhou,\" province\ ": Guangdong}}"; UserInfo user= (UserInfo) jsonconvert.deserializeobject (Jsondata, typeof (UserInfo));
2, read the value of a property in JSON can use the code:
Jobject jsonobj = Jobject.parse (jsondata); string name=jsonobj ["name"]. ToString (); string age=jsonobj ["Age"]. ToString (); string city= ((jobject) jsonobj ["addr"]) ["City"]. ToString (); string province= ((jobject) jsonobj ["addr"]) ["Province"]. ToString ();
3, explain the multilayer nested JSON, get the value of any property:
If you need to process the JSON string as follows:
{"Name": "Lily", "age": All, "addr": {"City": Guangzhou, "Province": Guangdong}};
Then let you enter an object, such as "City", the system will output "Guangzhou", enter "age", the output "23". Because JSON is nested in multiple levels, you need to iterate through the code as follows:
public string Getjsonvalue (jenumerable<jtoken> Jtoken, string key) { IEnumerator enumerator = Jtoken.getenumerator (); while (enumerator. MoveNext ()) { Jtoken JC = (Jtoken) Enumerator. Current; If (JC is Jobject | | ((Jproperty) JC). Value is Jobject) { return Getjsonvalue (JC. Children (), key); } else { if ((Jproperty) JC). Name = = key) { return (Jproperty) JC). Value.tostring (); }}} return null; }
Code to invoke Getjsonvalue:
String jsondata = "{\" name\ ": \" lily\ ", \" age\ ": 23,\" addr\ ": {\" city\ ": \" guangzhou\ ", \" province\ ": \" guangdong\ "}}"; Jobject jsonobj = Jobject.parse (Jsondata); Response.Write (Getjsonvalue (Jsonobj.children (), "province"));
If you have multiple layers of nested arrays, you can also use the following code:
String jsondata = "{\" addr\ ": [{\" city\ ": \" guangzhou\ ", \" province\ ": \" guangdong\ "},{\" city\ ": \" guiyang\ ", \" Province\ ": \" Guizhou\ "}]}"; Jobject jsonobj = Jobject.parse (Jsondata); Jarray jar = Jarray.parse (jsonobj["addr"]. ToString ()); Jobject j = jobject.parse (Jar[0]. ToString ()); Response.Write (j["City");
4. JSON to XML:
Copy the code code as follows:
String xmlstr= ((XmlDocument) Jsonconvert.deserializexmlnode (Jsondata)). Innerxml.tostring ();
The above is the whole content of this article, I hope that everyone's learning has helped, but also hope that we support topic.alibabacloud.com.