I Background introduction
1. What is json
JSON (JavaScript object notation) is a lightweight data exchange format. It is based on a subset of ECMAScript (js specification formulated by W3C) and uses a text format completely independent of the programming language to store and represent data. The concise and clear hierarchy makes JSON an ideal data exchange language. It is easy for people to read and write, but also easy for machine parsing and generation, and effectively improves the network transmission efficiency. Almost all programming languages have a library for parsing JSON—— Baidu Encyclopedia
Recommend an online analysis website;https://www.json.cn/
2.Json supports the following two data structures:
Set of key value pairs – this data structure is supported by various programming languages;
An ordered collection of list type values - this contains arrays, sets, vectors, or sequences, and so on;
3.Json has the following forms: (it's important to find out what form you need before you can call different methods for parsing)
3.1 object
For a "key / value" without sequence, an object starts with curly bracket "{" and ends with curly bracket "}". After each "key", there is a colon, and commas are used to separate multiple key value pairs. For example:
var user = {"name":"Manas","gender":"Male","birthday":"1987-8-8"}
3.2 array
Set the order of values. An array starts with brackets "[" and ends with brackets "]" and all values are separated by commas. In fact, it is the composition of the previous object set;
For example:
var userlist = [ {"user":{"name":"Manas","gender":"Male","birthday":"1987-8-8"}}, {"user":{"name":"Mohapatra","Male":"Female","birthday":"1987-7-7"}} ]
3.3 string
Any number of Unicode characters, marked with quotation marks and separated by backslashes.
(Note: quotation marks, commas and colons are half width symbols in English, and can only be double quotation marks)
For example:
var userlist = "{\"ID\":1,\"Name\":\"Manas\",\"Address\":\"India\"}"
II Use newtonsoft c# in unity json parsing json
1. What does json serialization and deserialization mean
Serialization: serialization is mainly for the convenience of transmission. The object to be transmitted is serialized into binary data stream, which is very efficient;
Deserialization: when receiving, it is converted into an object through deserialization, so as to achieve the effect of receiving transmission;
In short, serialization is used by the transmitting end to package the data to be transmitted into a standard transmission format, and deserialization is used by the receiving end to unpack the data to be received into the data format we need;
2. Define classes to accept json data
public class Student { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } public string Sex { get; set; } }
3. Generate json by serialization
JsonConvert.SerializeObject Method (Object, Type, Formatting, JsonSerializerSettings);
Newtonsoft.Json documentation:
https://www.newtonsoft.com/json/help/html/Overload_Newtonsoft_Json_JsonConvert_SerializeObject.htm
//Serialized object collection List<Student> oneList = new List<Student>() { new Student{ ID = 1, Name = "Wuhan University", Age = 260, Sex = "male" }, new Student{ ID = 2, Name = "Wu er", Age = 250, Sex = "male" }, new Student{ ID = 3, Name = "Wu San", Age = 240, Sex = "female" } }; //Define object string jsonData = JsonConvert.SerializeObject(oneList); //serialize Console.WriteLine(jsonData); //Display results Console.ReadLine();
4.json deserialization
Deserializing the entity object collection, as the name suggests, is to interpret json as an object, which can be directly passed c#'s object Property to access properties;
List<Student> twoList = JsonConvert.DeserializeObject<List<Student>>(jsonData); foreach(Student stu in twoList) { Console.WriteLine( string.Format("Student information ID:{0},full name:{1},Age:{2},Gender:{3}", stu.ID, stu.Name, stu.Age, stu.Sex));//Display results } Console.ReadLine();