JSON (JavaScript Object Notation) is a lightweight data exchange format. It is based on a subset of JavaScript (Standard ECMA-262 3rd Edition - December 1999). JSON adopts a completely language-independent text format, but also uses habits similar to the C language family (including C, C++, C#, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language. Easy to read and write, but also easy to machine parse and generate.
If you have used Json, it will be clear that Json can be divided into two parts:
1. Json Object(A collection of name/value pairs)
2. JSON Array (An ordered list of values)
There are many open source packages for parsing JSON and are widely used in various occasions, especially for network transmission.
This article introduces the use of LitJson and uses C# language to introduce the use of JSON, which can be used in C# applications, web programs, and Unity3d C# scripts.
Step 1: Download LitJson first and import the current project.
Step 2: Several examples of litJson
1. Use JsonData to process the generation of json:{"name":"peianandsky","age":28,"sex":"male"}
JsonData data = new JsonData(); data["name"] = "peiandsky"; data["age"] = 28; data["sex"] ="male"; string json1= ();
2. Nested objects in the object: {"name":"peianandsky","info":{"sex":"male","age":28}}
JsonData data2 = new JsonData(); data2["name"] = "peiandsky"; data2["info"] = new JsonData(); data2["info"]["sex"] = "male"; data2["info"]["age"] = 28; string json2 = ();
3. Parses Json from the above two methods to JsonData
JsonData jsonData2 = (json2); (jsonData2["name"] + " " + data2["info"]["sex"]);
4. Use JsonMapper to handle Json
Player player = new Player(); = "peiandsky"; = 23; = "male"; string json=(player);
5. Analyze json in 4
Player player2 = <Player>(json);
6. Generate Json in the most primitive way
Convert array to json:["one","two","three","four"]
JsonWriter writer = new JsonWriter(); (); ("one"); ("two"); ("three"); ("four"); ();
Convert compound objects to json string: {"book":{"title":"android game!","author":"pei","bookdetail":{"pages":429,"about":null}}}
JsonWriter writer2 = new JsonWriter(); (); ("book"); (); ("title"); ("android game!"); ("author"); ("pei"); ("bookdetail"); (); ("pages"); (429); ("about"); (null); (); (); (); (());
This method is very inconvenient and is not recommended to use.
When using LitJson, it is recommended to use JsonData and JsonMapper to handle Json encoding and parsing.
Person[] p_array = { p,p,p}; string json_array=(p_array); (json_array); JsonData pa = (json_array); (+" "+); for (int i = 0; i < ;i++ ) { (pa[i]["name"]+"-"+pa[i]["age"]+"-"+pa[i]["score"]+"-"+pa[i]["birth"]); int age = (pa[i]["age"].ToString()); (age); }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.