This article is reproduced from:
https://www.cnblogs.com/shang201215019/p/7907655.html
The big brother wrote very detailed. It is recommended to read the original text.
What is Json ?
Json [javascript object representation method],
It is a lightweight data exchange format, we can read and write it very easily,
And it is easy to convert and generate by computer, it is completely language independent.
Json supports the following two data structures:
- A collection of key-value pairs - a variety of different programming languages support this data structure;
- An ordered collection of list-typed values -- this includes arrays, sets, vectors, or sequences, etc.
Json has the following forms:
1. Object
An unordered "key/value", an object begins with curly braces "{" and ends with curly braces "}",
After each "key", there is a colon, and commas are used to separate multiple key-value pairs.
例如:var user = {"name":"Manas","gender":"Male","birthday":"1987-8-8"}
2. Array
To set the order of values, an array starts with square brackets "[" and ends with square brackets "]",
and all values are separated by commas,
E.g:
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: quotation marks, commas and colons are all half-width symbols in English, and can only be double quotation marks)
例如: var userlist = "{\"ID\":1,\"Name\":\"Manas\",\"Address\":\"India\"}"
How to use it in C#:
In C# we often use the following tools to parse the content in Json format
Newtonsoft.Json, is an open source Json serialization and deserialization tool in .Net, official address: http://www.newtonsoft.com/json .
Specific use:
1. Right-click the project => Nuget Package Management => Add Newtonsoft.Json
2. Introduce namespaces
1 using Newtonsoft.Json;
3. Define the class
public class Student { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } public string Sex { get; set; } }
4. Serialize and deserialize entity objects
// serialize the object Student one = new Student() { ID = 1, Name = "武松", Age = 250, Sex = "男" }; //Serialization string jsonData = JsonConvert.SerializeObject(one); Console.WriteLine(jsonData); //Display results Console.ReadLine(); // deserialize the object string str = "{\"ID\":2,\"Name\":\"鲁智深\",\"Age\":230,\"Sex\":\"男\"}"; // deserialize Student two = JsonConvert.DeserializeObject<Student>(str); Console.WriteLine( string.Format("Student ID: {0}, Name: {1}, Age: {2}, Gender: {3}", two.ID,two.Name, two.Age,two.Sex));//Display results Console.ReadLine();
Output result:
5. Serialize the collection of entity objects
// serialize the collection of objects List<Student> oneList = new List<Student>() { new Student{ ID = 1, Name = "武大", Age = 260, Sex = "男" }, new Student{ ID = 2, Name = "武二", Age = 250, Sex = "男" }, new Student{ ID = 3, Name = "武三", Age = 240, Sex = "女" } }; //Define the object string jsonData = JsonConvert.SerializeObject(oneList); //Serialization Console.WriteLine(jsonData); //Display results Console.ReadLine();
Show results:
6. Deserialize the collection of entity objects (the jsonData string in 5 is directly used here, forgive me for being lazy)
List<Student> twoList = JsonConvert.DeserializeObject<List<Student>>(jsonData); foreach(Student stu in twoList) { Console.WriteLine( string.Format("Student ID: {0}, Name: {1}, Age: {2}, Gender: {3}", stu.ID, stu.Name, stu.Age, stu.Sex));//Display results } Console.ReadLine();
Show results: