How to extract multi-layer nested json data in asp.net

高洛峰
Release: 2017-02-10 17:03:57
Original
1493 people have browsed it

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;
Copy after login

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;
}
Copy after login

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));
Copy after login

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();
Copy after login

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;
}
Copy after login

When calling:

string jsonData = "{\"name\":\"lily\",\"age\":23,\"addr\":{\"city\":\"guangzhou\",\"province\":\"guangdong\"}}";
JObject jsonObj = JObject.Parse(jsonData);
Response.Write(GetJsonValue(jsonObj.Children(), "province"));
Copy after login

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"]);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!