Method of extracting multilayer nested JSON data from ASP

Source: Internet
Author: User
This article mainly introduces the method of extracting multi-layer nested JSON data from ASP, and analyzes the steps and related operation skills of ASP. NET to parse the JSON format data in detail, which has a certain reference value, the need of friends can refer to

The example in this paper describes how to extract multiple layers of nested JSON data in ASP. Share to everyone for your reference, as follows:

Extract such JSON in. NET 2.0:

Copy the Code code as follows:

{"Name": "Lily", "Age": $, "addr": {"City": Guangzhou, "Province": Guangdong}}

Reference namespaces:

Using newtonsoft.json;using Newtonsoft.Json.Linq;

You can think of the JSON above 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 place of the parsing:

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 the Code code as follows:

{"Name": "Lily", "age": All, "addr": {"City": Guangzhou, "Province": Guangdong}};

Then let you enter an object, such as city, and then the system will output Guangzhou this value, then the JSON is dynamically generated, I would like to know that there is no way to read such JSON. (Note that JSON is multi-level nested.) )

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 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 the Code code as follows:

String xmlstr= ((XmlDocument) Jsonconvert.deserializexmlnode (Jsondata)). Innerxml.tostring ();

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.