Change article to: https://www.cnblogs.com/shang201215019/p/7907655.html
What is Json ?
Json "JavaScript Object Presentation method",
It is a lightweight data interchange format that we can easily read and write about,
And it is easily converted and generated by the computer, it is completely independent of the language.
JSON supports the following two types of data structures:
- The collection of key-value pairs--a variety of different programming languages, supports this data structure;
- A collection of ordered list-type values-this includes arrays, collections, vectors, or sequences, and so on.
JSON has several forms of expression:
1. Objects
A "key/value" with no order, an object begins with curly braces "{" and ends with curly braces "}".
After each "key", there is a colon, and a comma is used to separate multiple key-value pairs.
For example: var user = {"name": "Manas", "Gender": "Male", "Birthday": "1987-8-8"}
2. Arrays
Set the order of the values, an array begins with the brackets "[" and ends with the brackets "]",
And all values are separated by commas,
For example:
var userlist = [
{"User": {"name": "Manas", "Gender": "Male", "Birthday": "1987-8-8"}},
{"User": {"name": "Mohapatra", "Male": "Female", "Birthday": "1987-7-7"}}
]
3. String
any number of Unicode characters, marked with quotation marks, and delimited with backslashes.
( Note: The comma colon is the English-language half-width symbol and can only be double quotation marks)
For example: var userlist = "{\" id\ ": 1,\" name\ ": \" Manas\ ", \" address\ ": \" India\ "}"
How to use C # in detail:
In C # We often use the following tools to parse content in JSON format
Newtonsoft.json, an open source Json serialization and deserialization tool in. NET, official address: Http://www.newtonsoft.com/json.
Specific use:
1. Right-click Project =>nuget Package Management and add Newtonsoft.json
2. Introduce namespaces
Using Newtonsoft.json;
3. Defining classes
Publicclass Student {pub LIC int ID {get; setpublic string Name {get ; setpublic int Age {get; Span style= "color: #0000ff;" >setpublic string Sex {get; set
4. Serialization and deserialization of entity objects
//Serialized object Student One =NewStudent () {ID =1, Name ="Wu Song", age =, Sex ="Man"};//Serialization ofString jsondata =Jsonconvert.serializeobject (one); Console.WriteLine (Jsondata);// Console.ReadLine (); string str = " Span style= "color: #800000;" >{\ "id\": 2,\ "name\": \ "Lu zhishen \", \ "age\": 230,\ "sex\": \ "male \"} (STR); Console.WriteLine (string. Format ( " Student Information id:{0}, Name: {1}, Age: {2}, Gender: {3} " Two.id,two. Name, both. Age,two. Sex)); // show results console.readline ();
Output Result:
5. Serializing a collection of entity objects
//Serializing a collection of objects list<student> Onelist =New list<student>() {New student{ID =1, Name ="Wuhan University", age =260, Sex ="Man"},New student{ID =2, Name ="Takeji", age =, Sex =" male new student{ID = 3, Name = Wu San 240, Sex = Span style= "color: #800000;" > " female }}; // define object string Jsondata = Jsonconvert.serializeobject (onelist); // serialization // show results console.readline ();
Show Results:
6. The collection of deserialized entity objects (the jsondata string in the 5 is used directly, excuse me for stealing some lazy)
list<student> twolist = jsonconvert.deserializeobject<list<student>>(jsonData); in twolist) { Console.WriteLine ( String. Format (" Student Information id:{0}, Name: {1}, Age: {2}, Gender: {3}", Stu.id, Stu. Name, Stu. Age, Stu. Sex)); // } console.readline ();
Show Results:
Serialization and deserialization of JSON strings in C #