1. JSON syntax is a subset of JavaScript object notation syntax.
The data is in a name/value pair: The name is a string and is represented by double quotation marks. Values can be: numbers (integers or floating-point numbers), strings (in double quotes), arrays (in square brackets), objects (in curly brackets), true/false/null.
Data is separated by commas:
Curly braces Save objects: objects can contain any data, including arrays.
Square brackets Save an array: numbers can contain objects.
For example:
{
"Students": [
{
"Name": "Zhang San",
"Age": 12
},
{
"Name": "John Doe",
"Age": 14
}
]
}
2. If the JSON contains escape characters, you need to escape.
C # Operations JSON
The JSON file is read into memory as a string. NET operation JSON is the generation and parsing of JSON strings. There are usually several ways to manipulate JSON:
Common Way
This approach is to use Open source class library Newtonsoft.json (http://json.codeplex.com/). After downloading, you can use it to join the project. You can usually use Jobject, Jsonreader, jsonwriter processing. This approach is the most versatile and flexible, and can be modified at any time.
(1) Use Jsonreader to read the JSON string:
string Jsontext [email protected] "{" "Input" ":" "Value" "," "Output" ":" "Result" "}";
Jsonreader reader = new JsonTextReader (Newstringreader (Jsontext));
while (reader. Read ())
{
Console.WriteLine (reader. Tokentype + "\t\t" + reader. Valuetype+ "\t\t" + reader. Value);
}
(2) write a string using Jsonwriter:
StringWriter SW = new StringWriter ();
Jsonwriter writer = new JsonTextWriter (SW);
Writer. Writestartobject ();
Writer. Writepropertyname ("input");
Writer. WriteValue ("value");
Writer. Writepropertyname ("Output");
Writer. WriteValue ("result");
Writer. Writeendobject ();
Writer. Flush ();
String Jsontext =SW. Getstringbuilder (). ToString ();
Console.WriteLine (Jsontext);
(3) Read and write strings using Jobject:
Jobject Jo = Jobject.parse (jsontext);
String[] Values =jo. Properties (). Select (item = Item. Value.tostring ()). ToArray ();
(4) Use Jsonserializer to read and write objects (based on Jsonwriter and Jsonreader):
Array-type data
String jsonArrayText1 = "[{' A ': ' A1 ', ' B ': ' B1 '},{' a ': ' A2 ', ' B ': ' B2 '}]";
Jarray ja = (jarray) jsonconvert.deserializeobject (JSONARRAYTEXT1);
String JA1A =ja[1]["a"]. ToString ();
Or
Jobject o = (jobject) ja[1];
String OA = o["a"]. ToString ();
Nested formats
String jsontext ="{\" beijing\ ": {\" zone\ ": \" Haidian \ ", \" zone_en\ ": \" haidian\ "}}";
Jobject Jo = (jobject) jsonconvert.deserializeobject (Jsontext);
string Zone =jo["Beijing" ["zone"]. ToString ();
String zone_en =jo["Beijing" ["Zone_en"]. ToString ();
Custom class Project
Project P = new Project () {Input = "stone", Output = "Gold"};
Jsonserializer serializer = Newjsonserializer ();
StringWriter SW = new StringWriter ();
Serializer. Serialize (Newjsontextwriter (SW), p);
Console.WriteLine (SW. Getstringbuilder (). ToString ());
StringReader sr = Newstringreader (@ "{" "Input" ":" "Stone" "," "Output" ":" "Gold" "}");
Project P1 = (project) serializer. Deserialize (new JsonTextReader (SR), typeof (Project));
Console.WriteLine (P1. Input + "=" + p1. Output);
The code above is based on the following project class definition:
Class Project
{
public string Input {get; set;}
public string Output {get; set;}
}
C # parsing JSON-formatted data