This article mainly introduces the method of asp.net to extract multi-layer nested json data. It analyzes in detail the steps and related operation skills of asp.net to parse json format data in the form of examples. It has certain reference value. Friends in need can refer to
The example in this article describes the method of extracting multi-layer nested json data in asp.net. Share it with everyone for your reference, the details are as follows:
Extract json like this in .net 2.0:
Copy code The code is as follows:
{"name":"lily","age":23,"addr":{"city":guangzhou,"province":guangdong}}
Reference namespace:
using Newtonsoft.Json; using Newtonsoft.Json.Linq;
You can think of 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 like this in the parsing place:
string jsonData="{\"name\":\"lily\",\"age\":23,\"addr\":{\"city\":guangzhou,\"province\":guangdong}}"; UserInfo user=(UserInfo)JsonConvert.DeserializeObject(jsonData, typeof(UserInfo));
To get the value of City, just: user.addr.City;
This will work too
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 can this json be dynamic? For example, let you input a json, such as
Copy code The code is as follows:
{"name":"lily","age":23," addr":{"city":guangzhou,"province":guangdong}};
Then let you input an object, such as city, and then the system will output the value of guangzhou. In this case, json is dynamically generated Yes, I would like to know if there is any way to read such json. (Note that json is multi-level nested.)
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; }
When calling:
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 levels 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 The code is as follows:
string xmlstr=((XmlDocument)JsonConvert.DeserializeXmlNode(jsonData)).InnerXml.ToString();
More For related articles on the method of extracting multi-layer nested json data in asp.net, please pay attention to the PHP Chinese website!

This article explores the challenges of NULL pointer dereferences in C. It argues that the problem isn't NULL itself, but its misuse. The article details best practices for preventing dereferences, including pre-dereference checks, pointer initiali

This article explains how to create newline characters in C using the \n escape sequence within printf and puts functions. It details the functionality and provides code examples demonstrating its use for line breaks in output.

This article guides beginners on choosing a C compiler. It argues that GCC, due to its ease of use, wide availability, and extensive resources, is best for beginners. However, it also compares GCC, Clang, MSVC, and TCC, highlighting their differenc

This article emphasizes the continued importance of NULL in modern C programming. Despite advancements, NULL remains crucial for explicit pointer management, preventing segmentation faults by marking the absence of a valid memory address. Best prac

This article reviews online C compilers for beginners, focusing on ease of use and debugging capabilities. OnlineGDB and Repl.it are highlighted for their user-friendly interfaces and helpful debugging tools. Other options like Programiz and Compil

This article compares online C programming platforms, highlighting differences in features like debugging tools, IDE functionality, standard compliance, and memory/execution limits. It argues that the "best" platform depends on user needs,

This article discusses efficient code copying in C IDEs. It emphasizes that copying is an IDE function, not a compiler feature, and details strategies for improved efficiency, including using IDE selection tools, code folding, search/replace, templa

This article troubleshoots missing output windows in C program compilation. It examines causes like failing to run the executable, program errors, incorrect compiler settings, background processes, and rapid program termination. Solutions involve ch


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
