JSON has been briefly described in the JSON entry-level learning summary--json data structure, JSON string array data styles are probably like this:
Today, because of project requirements (ASP. NET Web site, the foreground passes JSON data to the background, and the JSON is processed in detail, see the blog
Project Experience--get foreground data through JS Pass JSON data to a generic handler and parse JSON data to write JSON data from the foreground to the database table
"), you need to deserialize the JSON data that is passed. So I found many examples of JSON deserialization from the Internet, finally decided to use Newtonsoft.Json.dll to deserialize the JSON string, of course it can also serialize JSON string,Newtonsoft.Json.dll download link !
to deserialize a Json string using Newtonsoft.Json.dll:
1. Adding references to Newtonsoft.Json.dll
Copy the Newtonsoft.Json.dll file to the bin directory of the interface layer, then right-click on the bin, pop up the "Add Reference" page, then click "Browse", then find the Bin folder of the interface layer, locate the Newtonsoft.Json.dll file, then click "Add" , the reference was added successfully!
2. In the interface that requires JSON string processing, reference the JSON space and process the JSON
The online solution is roughly as follows:
Method 1:
[CSharp]View PlainCopyprint?
- <span style="FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >//Referencing the space used by JSON parsing
- Using Newtonsoft.json;
- Using System.Text;
- Defines a variable string array
- StringBuilder sb = new StringBuilder ();
- Defines a JSON string str
- String str = "[{ID: ' 1 ', Name: ' John ', other:[{age: ' + ', Sex: ' 0 '}]},{id: ' 2 ', ' Name: ' Good ', Other:[{age: ' $ ', Sex: ' 1 '}] }]";
- Deserialization of JSON data
- Javascriptarray JavaScript = (javascriptarray) javascriptconvert.deserializeobject (str);
- Reads the deserialized JSON data sequentially and writes the data to the variable string array
- for (int i = 0; i < JavaScript. Count; i++)
- {
- //
- Javascriptobject obj = (javascriptobject) javascript[i];
- //variable string array add data
- Sb. Append ("ID:" + obj["id"]. ToString ());
- Sb. Append ("Name:" + obj["name"]. ToString ());
- //deserialization of JSON data
- Javascriptarray json = (javascriptarray) obj["Other"];
- reads the deserialized JSON data sequentially and writes the data to the variable string array
- For (int j = 0; J < JSON. Count; J + +)
- {
- Javascriptobject jsonobj = (javascriptobject) json[j];
- Sb. Append ("Age:" + jsonobj["age"]. ToString ());
- Sb. Append ("Sex:" + jsonobj["Sex"]. ToString ());
- }
- } </span>
Another way is to:
[CSharp]View PlainCopy print?
- Defines a JSON string str
- String jsontext = "[{' A ': ' AAA ', ' B ': ' BBB ', ' C ': ' CCC '},{' a ': ' Aaa2 ', ' B ': ' Bbb2 ', ' C ': ' CCC2 '}]";
- //deserialization of JSON data
- Jsonreader reader = new Jsonreader (new StringReader (Jsontext));
- //Read the deserialized JSON data sequentially
- While (reader. Read ())
- {
- TextBox1.Text + = "Tokentype =" + Reader. Tokentype + "ValueType =" + Reader. ValueType + "Value =" + Reader. Value + "\ r \ n";
- }
The first method compiles direct error, does not recognize Javascriptarray,javascriptobject, even Javascriptconvert method also wood has! Well, the second kind of head office, the hint:
Error 2 Cannot create an instance of an abstract class or interface "Newtonsoft.Json.JsonReader"
What's going on???
Later in the search of the article, found that the new version of Javascriptarray into Jarray, along this line of thought, found the following changes:
[CSharp]View PlainCopyprint?
- <span style="FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" >javascriptarray--->jarray
- Javascriptconvert--->jsonconvert
- Javascriptobject--->JObject</span>
In fact, such changes are more standardized!
Here's how to deserialize a Json string in a new version of Newtonsoft.Json.dll
1. Direct deserialization of JSON strings
[CSharp]View PlainCopyprint?
- Reference to the space used to serialize, deserialize JSON strings
- Using Newtonsoft.json;
- Using Newtonsoft.Json.Linq;
- //define a JSON string
- String jsontext = "[{' A ': ' AAA ', ' B ': ' BBB ', ' C ': ' CCC '},{' a ': ' Aaa2 ', ' B ': ' Bbb2 ', ' C ': ' CCC2 '}]";
- Deserializing JSON strings
- Jarray ja = (jarray) jsonconvert.deserializeobject (Jsontext);
- //Converts a deserialized JSON string into an object
- Jobject o = (jobject) ja[1];
- Reading the values in an object
- Console.WriteLine (o["a"]);
- Console.WriteLine (ja[1]["a"]);
2. Define a list of objects and deserialize the JSON string
2.1 Add a class that defines an object
[CSharp]View PlainCopyprint?
- Using System;
- Using System.Collections.Generic;
- Using System.Linq;
- Using System.Web;
- <summary>
- Summary description of Customer
- </summary>
- Public class Customer
- {
- //No changes before and after serialization
- public string a
- { get; set;}
- //Set and reset before and after serialization
- public string B
- { get; set;}
- //set to NULL, but populated after serialization
- public string C
- { get; set;}
- public string Other
- { get; set;}
- Public Customer ()
- {
- //
- //todo: Add constructor logic here
- //
- A = "";
- b = "";
- c = "";
- other = null;
- }
- }
2.2 Deserializing JSON strings
[CSharp]View PlainCopyprint?
- Reference to the space used to serialize, deserialize JSON strings
- Using Newtonsoft.json;
- Using Newtonsoft.Json.Linq;
- Define a JSON string
- String jsontext = "[{' A ': ' AAA ', ' B ': ' BBB ', ' C ': ' CCC '},{' a ': ' Aaa2 ', ' B ': ' Bbb2 ', ' C ': ' CCC2 '}]";
- Deserializes a JSON string into a list of JSON strings
- list<customer> _list = jsonconvert.deserializeobject<list<customer>> (JsonText);
- Reading a value from a list
- Console.WriteLine (_LIST[1].A);
- foreach (Customer C in _list)
- {
- Console.WriteLine (C.C);
- }
I feel that the two methods of deserializing JSON are very good, in fact, there are many ways to deserialize JSON, this article only with the help of Newtonsoft.Json.dll this tool!
Newtonsoft.Json.dll deserialization of Json strings