Sample code for C to parse JSON using LitJson

  • 2021-11-30 01:13:06
  • OfStack

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 uses 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. It is easy for people to read and write, and it is also easy for machine analysis and generation.

If you have ever used Json, it is 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 analyzing JSON, which are widely used in various occasions, especially for network transmission.

This article describes the use of LitJson and the use of JSON in the C # language, which can be used in C # applications, web programs, and Unity 3d C # scripts.

Step 1: Download LitJson and import the current project.

Step 2: Several instances of litJson

1. Use JsonData to process generating json: {"name": "peiandsky", "age": 28, "sex": "male"}


JsonData data = new JsonData();

    data["name"] = "peiandsky";

    data["age"] = 28;

    data["sex"] ="male";

    string json1= data.ToJson();

2. Nested objects in objects: {"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 modes into JsonData


JsonData jsonData2 = JsonMapper.ToObject(json2);
Debug.Log(jsonData2["name"] + "  " + data2["info"]["sex"]);

4. Use JsonMapper to process Json


Player player = new Player();

    player.name = "peiandsky";

    player.age = 23;

    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", "two", "three", "four"]


 JsonWriter writer = new JsonWriter();

    writer.WriteArrayStart();

    writer.Write("one");

    writer.Write("two");

    writer.Write("three");

    writer.Write("four");

    writer.WriteArrayEnd();

Convert a composite object to an 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.

In using LitJson, it is recommended to use JsonData and JsonMapper to handle the encoding and parsing of Json.


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 articles: