Detailed example code for C # parsing JSON using Litjson

Source: Internet
Author: User
Tags tojson
This article mainly introduces the C # use Litjson parsing JSON sample code, small series think very good, now share to everyone, also give you a reference. Let's take a look at it with a little knitting.

JSON (JavaScript Object Notation) is a lightweight data interchange format. It is based on a subset of JavaScript (standard ECMA-262 3rd edition-december 1999). JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (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've ever used JSON, it's 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 that parse JSON, which are widely used in a variety of situations, especially longer than network transmissions.

This article describes the use of Litjson and describes the use of JSON in the C # language, which can be used in C # applications, Web programs, and Unity3d C # scripts.

First step: Download Litjson and import the current project first.

Step two: Several instances of Litjson

1. Use Jsondata to process the build json:{"name": "Peiandsky", "age": +, "sex": "Male"}

Jsondata data = new Jsondata ();    data["name"] = "Peiandsky";    Data["Age" =;    data["sex"] = "male";    String json1= data. ToJson ();

2. Nested objects in object: {"name": "Peiandsky", "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 = data2. ToJson ();

3. Parse the JSON of the above two ways to Jsondata

Jsondata jsonData2 = Jsonmapper.toobject (json2);D ebug. Log (jsondata2["name"] + "  " + data2["info" ["Sex"]);

4. Using Jsonmapper to process JSON

Player player = new player ();    Player.name = "Peiandsky";    Player.age =;    Player.sex = "male";    String Json=jsonmapper.tojson (player);

5. Parsing JSON in 4

Player player2 = jsonmapper.toobject<player> (JSON);

6. Generate JSON in the most primitive way

Convert array to json:["one", "one", "three", "four"]

Jsonwriter writer = new Jsonwriter ();    Writer. Writearraystart ();    Writer. Write ("one");    Writer. Write ("both");    Writer. Write ("three");    Writer. Write ("four");    Writer. Writearrayend ();

Convert a composite object to a JSON string: {"book": {"title": "Android game!", "Author": "Pei", "Bookdetail": {"pages": 429, "about": null}}

Jsonwriter writer2 = new Jsonwriter ();     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 ());

This method is very inconvenient and is not recommended for use.

In the use of Litjson, it is recommended to use Jsondata,jsonmapper to handle JSON encoding and parsing.

person[] P_array = {p,p,p};    String Json_array=jsonmapper.tojson (P_array);     Debug.Log (Json_array);     Jsondata pa = jsonmapper.toobject (Json_array); Debug.Log (Pa. Isarray+ "" +pa.     Count); for (int i = 0; i < Pa.      count;i++) {Debug.Log (pa[i]["name"]+ "-" +pa[i]["Age"]+ "-" +pa[i]["score"]+ "-" +pa[i]["birth"]); int age = Int. Parse (pa[i]["age").      ToString ());    Debug.Log (age); }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.