asp.net method for extracting multilayer nested JSON data _ practical Tips

Source: Internet
Author: User

The example in this article describes the ASP.net method for extracting multiple layers of nested JSON data. Share to everyone for your reference, specific as follows:

Extract such JSON in. NET 2.0:

Copy Code code as follows:
{"Name": "Lily", "Age": "addr": {"City": Guangzhou, "Province": Guangdong}}

Referencing namespaces:

Using Newtonsoft.json;
Using Newtonsoft.Json.Linq;

You can think of the above JSON as an object. You just 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 parse place:

String Jsondata= "{\ name\": \ "lily\", \ "age\": 23,\ "addr\": {\ "city\": Guangzhou,\ "province\": Guangdong}} ";
UserInfo user= (UserInfo) jsonconvert.deserializeobject (Jsondata, typeof (UserInfo));

Get the value of city as long as: user.addr.City;

This can be done.

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 a JSON, such as

Copy Code code as follows:
{"Name": "Lily", "Age": "addr": {"City": Guangzhou, "Province": Guangdong}};

Then let you enter an object, such as city, and the system will output the value of Guangzhou, so that JSON is dynamically generated, and I want to know if there is a way to read this JSON. (Note that JSON is nested in multiple levels.) )

You use the 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 there are multiple layers 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 code as follows:
String xmlstr= ((XmlDocument) Jsonconvert.deserializexmlnode (Jsondata)). Innerxml.tostring ();

PS: About JSON Format data Manipulation Small series here we recommend several online tools for free use, I believe that in future development can be used:

Online JSON code inspection, inspection, landscaping, formatting tools:
Http://tools.jb51.net/code/json

Online Xml/json Mutual Conversion tool:
Http://tools.jb51.net/code/xmljson

C Language Style/html/css/json code formatting landscaping Tools:
Http://tools.jb51.net/code/ccode_html_css_json

JSON code online Format/beautify/compress/edit/Convert tools:
Http://tools.jb51.net/code/jsoncodeformat

More interested readers of asp.net related content can view the site topics: "asp.net operation JSON tips summary", "asp.net string operation tips Summary", "ASP.net Operation XML Skills summary", "asp.net file Operation skills Summary", " asp.net Ajax Skills Summary topic and the "ASP.net cache operation skills Summary."

I hope this article will help you to ASP.net program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.