asp. net Method for Extracting Multi layer Nested json Data

  • 2021-07-24 10:48:03
  • OfStack

In this paper, the method of extracting multi-layer nested json data by asp. net is described as an example. Share it for your reference, as follows:

Extract such an json in. net 2.0:

{"name":"lily","age":23,"addr":{"city":guangzhou,"province":guangdong}}

Reference Namespace:


using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

Think of the above JSON as an object. You only 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 this in the parsing place:


string jsonData="{\"name\":\"lily\",\"age\":23,\"addr\":{\"city\":guangzhou,\"province\":guangdong}}";
UserInfo user=(UserInfo)JsonConvert.DeserializeObject(jsonData, typeof(UserInfo));

As long as the value of City is obtained: user. addr. City;

This implementation will do


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 is this json dynamic? For example, let you enter 1 json, such as

{"name":"lily","age":23,"addr":{"city":guangzhou,"province":guangdong}};

Then let you enter an object, such as city, and then the system will output the value of guangzhou. In this case, json is dynamically generated. I want to know if there is a method to read such json. (Note that json is nested at multiple levels.)

Just 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;
}

At the time of the call:


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 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:

string xmlstr=((XmlDocument)JsonConvert.DeserializeXmlNode(jsonData)).InnerXml.ToString();

PS: About json format data operation This site here recommend several online tools for everyone to use free of charge, I believe in the future development can come in handy:

Online JSON code verification, verification, beautification and formatting tools:
http://tools.ofstack.com/code/json

Online XML/JSON Interconversion Tool:
http://tools.ofstack.com/code/xmljson

C Language Style/HTML/CSS/json Code Formatting and Beautification Tool:
http://tools.ofstack.com/code/ccode_html_css_json

json code online formatting/beautification/compression/editing/conversion tool:
http://tools.ofstack.com/code/jsoncodeformat

More readers interested in asp. net can check the topics of this site: "asp. net Operation json Skills Summary", "asp. net String Operation Skills Summary", "asp. net Operation XML Skills Summary", "asp. net File Operation Skills Summary", "asp. net ajax Skills Summary" and "asp. net Cache Operation Skills Summary".

I hope this article is helpful to everyone's asp. net programming.


Related articles: