SoFunction
Updated on 2025-03-10

Methods for extracting multi-layer nested json data

This article describes the method of extracting multi-layer nested json data. Share it for your reference, as follows:

Extract such json in .net 2.0:

Copy the codeThe code is as follows:
{"name":"lily","age":23,"addr":{"city":guangzhou,"province":guangdong}}

Reference namespace:

using ;
using ;

You can regard 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 it in the parsing place:

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

Get the City value as long as:;

This is OK to achieve

JObject jsonObj = (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 does this json be dynamic? For example, let you enter a json,

Copy the codeThe code is as follows:
{"name":"lily","age":23,"addr":{"city":guangzhou,"province":guangdong}};

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

Just use traversal

public string GetJsonValue(JEnumerable<JToken> jToken,string key)
{
IEnumerator enumerator = ();
while (())
{
JToken jc = (JToken);
if (jc is JObject||((JProperty)jc).Value is JObject)
{
return GetJsonValue((), key);
}
else
{
if (((JProperty)jc).Name == key)
{
return ((JProperty)jc).();
}
}
}
return null;
}

When calling:

string jsonData = "{\"name\":\"lily\",\"age\":23,\"addr\":{\"city\":\"guangzhou\",\"province\":\"guangdong\"}}";
JObject jsonObj = (jsonData);
(GetJsonValue((), "province"));

If there are multiple nested arrays

string jsonData = "{\"addr\":[{\"city\":\"guangzhou\",\"province\":\"guangdong\"},{\"city\":\"guiyang\",\"province\":\"guizhou\"}]}";
JObject jsonObj = (jsonData);
JArray jar = (jsonObj["addr"].ToString());
JObject j = (jar[0].ToString());
(j["city"]);

JSON to XML:

Copy the codeThe code is as follows:
string xmlstr=((XmlDocument)(jsonData)).();

PS: Regarding json format data operation, the editor here recommends several online tools for everyone to use for free. I believe that it will come in handy in future development:

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

Online XML/JSON mutual conversion tool:
http://tools./code/xmljson

C language style/HTML/CSS/json code formatting and beautification tools:
http://tools./code/ccode_html_css_json

json code online formatting/beautification/compression/editing/converting tools:
http://tools./code/jsoncodeformat

For more information about relevant content, please check out the topic of this site:Summary of operation json skills》、《Summary of string operation techniques》、《Summary of operating XML skills》、《Summary of file operation skills》、《Ajax tips summary topic"and"Summary of cache operation skills》。

I hope this article will be helpful to everyone's programming.