Detailed description of ASP. NET's method for extracting multi-layer nested json data, asp. netjson

Source: Internet
Author: User

Detailed description of ASP. NET's method for extracting multi-layer nested json data, asp. netjson

This example describes how ASP. NET uses the third-party class library Newtonsoft. Json to extract multi-layer nested json data. The example is as follows.

Assume that the json string to be extracted is as follows:

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

Reference namespace first:

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

You can regard the preceding json string as an object, as long as you write the corresponding class (if you use VS2013 for development, you can quickly convert a JSON string to an object class by editing, selecting, and pasting json as a class)

 public class UserInfo {  public string name;  public int age;  public address addr; } public class address {  public string city;  public string province; }

1. The code for converting a json string to an object is as follows:

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

2. You can use the following code to read the value of an attribute in json:

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();

3. interpret multi-layer nested json to obtain the value of any attribute:

If the json string to be processed is as follows:

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

Then let you input an object, such as "city", the system will output "guangzhou", input "age", and output "23 ". Json is multi-level nested and needs to be traversed one by one. The Code is as follows:

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

Code for calling GetJsonValue:

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

You can also use the following code for multi-layer 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"]);

4. Convert JSON to XML:

Copy codeThe Code is as follows:
String xmlstr = (XmlDocument) JsonConvert. DeserializeXmlNode (jsonData). InnerXml. ToString ();

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

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.