Key Words: Json,c#,litjson,unity3d
Content: unity3d, Some general usage of Json.
Definition: (bing Dictionary)
The Json (JavaScript Object Notation) is a lightweight data exchange language that is conceived and designed by Douglas Kest ford, and is text-based and easy to Read. Although JSON is a subset of Javascript , JSON is a language-independent text format and uses some of the same habits as the C language family.
JSON can be divided into two parts:
-
- Json Object (A Collection of Name/value Pairs)
- Json Array (an ordered list of values);
Get ready:
You need to download Litjson and import it into your project.
Example 1---generation
{"name": "peiandsky", "age": +, "sex": "male"}
New Jsondata (); data["name""Peiandsky"; data["Age"; data["sex"] ="male"; string json1= Data. ToJson ();
Example 2---nested objects in an object
{"name": "peiandsky", "info": {"sex": "male", "age": 28}}
Jsondata data2 =NewJsondata (); data2["name"] ="Peiandsky"; data2["Info"] =NewJsondata (); data2["Info"]["Sex"] ="male"; data2["Info"][" age"] = -;stringJson2 = data2. ToJson ();
Note: "info" itself is a Json.
Example 3---json parsing to Jsondata
Jsondata jsonData2 = jsonmapper.toobject (json2);D Ebug. Log (jsondata2["name" "" + data2["info "] ["sex"]);
Example 3---processing JSON with Jsonmapper
New Player (); " Peiandsky " ; at ; " male " ; string Json=jsonmapper.tojson (player);
---parsing (json is the data taken)
Player player2 = jsonmapper.toobject<player> (json);
Note: when JSON is fetching data for us, each data type in theplayer is consistent with the json, throws an error when inconsistent, and adjusts the data type of the player in the resolution according to the error message.
Example 4---generating a JSON statement in the original way
{"one", "one", "three", "four"}
New Jsonwriter (); Writer. Writearraystart (); Writer. Write ("one"); Writer. Write ("both"); Writer. Write ("three"); Writer. Write ("four"); Writer. Writearrayend ();
Example 5---converting a composite object to JSON
{"book": {"title": "android game!", "author": "pei", "bookdetail": {"pages": 429, ' about ': null}}}
Jsonwriter Writer2 =NewJsonwriter (); writer2. Writeobjectstart (); writer2. Writepropertyname (" book"); writer2. Writeobjectstart (); writer2. Writepropertyname ("title"); writer2. Write ("Android game!"); writer2. Writepropertyname ("author"); writer2. Write ("Pei"); writer2. Writepropertyname ("Bookdetail"); writer2. Writeobjectstart (); writer2. Writepropertyname ("pages"); writer2. Write (429); writer2. Writepropertyname (" about"); writer2. Write (NULL); writer2. Writeobjectend (); writer2. Writeobjectend (); writer2. Writeobjectend (); Debug.Log (writer2. ToString ());
Self-summary:
JSON makes data transfer easy, simple and clear.
JSON can be nested in JSON
Do not know the content of the time, the data taken directly xxx.tostring () can directly see the content, according to the content of the re-write parsing.
[game development-learning notes] rookie slowly flying (one)-json in Unity3d