C # parse Json,

Source: Internet
Author: User

C # parse Json,

1. parse JSON data

Tool: Newtonsoft. Json class library/dll

Currently, I only use this class library to parse json data. This class library can be used to directly serialize and deserialize C # And JSON.

First, I copied a piece of json data written on the Internet in the local text file txt, such as (the path of the txt file is saved in disk D ):

Now, let's parse the json data in the txt text format! For a good demonstration, I will directly create a console Code as follows:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using Newtonsoft. json; using System. IO; using Newtonsoft. json. linq; namespace ConsoleApplication1 {class Program {static void Main (string [] args) {JsonSerializer serializer = new JsonSerializer (); using (StreamReader reader = new StreamReader (@ "d: \ json.txt ") using (JsonReader jsreader = new JsonTextReader (reader) {JObject jo = (JObject) serializer. deserialize (jsreader); // deserialization of json is then converted to JObject Console. writeLine (jo. toString ();} Console. read ();}}}

Running result:

2. Linq To Json

Main classes of linq to json:

1. JObejct: json object

2. JArray: used to operate json Arrays

3. JValue: Value in the array

4. JProperty: The property of a json object, which usually exists in a dictionary similar to a key or value.

5. JToken: used to store the result value of the linq Query

The following example shows how to use JObejct to create json data and output the following code:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using Newtonsoft. json; using System. IO; using Newtonsoft. json. linq; namespace ConsoleApplication1 {class Program {static void Main (string [] args) {JObject jo = new JObject (); jo. add (new JProperty ("name", "Zhang huimei"); jo. add (new JProperty ("sex", "female"); jo. add (new JProperty ("age", "32"); jo. add (new JProperty ("teachar", new JObject (new JProperty ("name", "Zhang Yusheng"), new JProperty ("sex", "male "), new JProperty ("age", "30"); Console. writeLine (jo. toString (); Console. readLine ();}}}

On this basis, modify the code:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using Newtonsoft. json; using System. IO; using Newtonsoft. json. linq; namespace ConsoleApplication1 {class Program {static void Main (string [] args) {JObject jo = new JObject (); jo. add (new JProperty ("name", "Zhang huimei"); jo. add (new JProperty ("sex", "female"); jo. add (new JProperty ("age", "32"); jo. add (new JProperty ("teachar", new JObject (new JProperty ("name", "Zhang Yusheng"), new JProperty ("sex", "male "), new JProperty ("age", "30"); JToken Tteacher = jo ["teachar"]; Console. writeLine (Tteacher. toString (); Console. readLine ();}}}

From this we can know that the value returned by the JToken is actually the value part of the corresponding key. Then we can use linq to json to prepare a string and use linq to query the name of Wang Lihong's friend:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using Newtonsoft. json; using System. IO; using Newtonsoft. json. linq; namespace ConsoleApplication1 {class Program {static void Main (string [] args) {string jsonstr = "{\" Name \ ": \" Wang Li Hong \", \ "Age \": 34, \ "friends \": [{\ "Name \": \ "Lin Junjie \", \ "Age \": 30 }, {\ "Name \": \ "Zhang huimei \", \ "Age \": 29}]} "; JObject jo = JObject. parse (jsonstr); var a = from B in jo ["friends"]. children () select (string) B ["Name"]; foreach (var item in a) {Console. writeLine (item);} Console. read ();}}}

Here is a simple introduction to Json parsing... Please correct me if there is anything wrong or you need to correct it.

 

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.