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.