This article mainly introduces the method of asp.net to extract multi-layer nested json data. It analyzes in detail the steps and related operation skills of asp.net to parse json format data in the form of examples. It has certain reference value. Friends in need can refer to
The example in this article describes the method of extracting multi-layer nested json data in asp.net. Share it with everyone for your reference, the details are as follows:
Extract json like this in .net 2.0:
Copy code The code is as follows:
{"name":"lily","age":23,"addr":{"city":guangzhou,"province":guangdong}}
Reference namespace:
using Newtonsoft.Json; using Newtonsoft.Json.Linq;
You can think of the above JSON as an object. You just need to write the corresponding class
public class UserInfo { public string name; public int age; public address addr; } public class address { public string city; public string province; }
Then write like this in the parsing place:
string jsonData="{\"name\":\"lily\",\"age\":23,\"addr\":{\"city\":guangzhou,\"province\":guangdong}}"; UserInfo user=(UserInfo)JsonConvert.DeserializeObject(jsonData, typeof(UserInfo));
To get the value of City, just: user.addr.City;
This will work too
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();
How can this json be dynamic? For example, let you input a json, such as
Copy code The code is as follows:
{"name":"lily","age":23," addr":{"city":guangzhou,"province":guangdong}};
Then let you input an object, such as city, and then the system will output the value of guangzhou. In this case, json is dynamically generated Yes, I would like to know if there is any way to read such json. (Note that json is multi-level nested.)
Use traversal
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; }
When calling:
string jsonData = "{\"name\":\"lily\",\"age\":23,\"addr\":{\"city\":\"guangzhou\",\"province\":\"guangdong\"}}"; JObject jsonObj = JObject.Parse(jsonData); Response.Write(GetJsonValue(jsonObj.Children(), "province"));
If there are multiple levels of nested arrays
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"]);
JSON to XML:
Copy code The code is as follows:
string xmlstr=((XmlDocument)JsonConvert.DeserializeXmlNode(jsonData)).InnerXml.ToString();
More For related articles on the method of extracting multi-layer nested json data in asp.net, please pay attention to the PHP Chinese website!