JSON interpretation and use of cJSON API

1. What is JSON?

        Official description:

  • JSON refers to JavaScript Object Notation
  • JSON is a lightweight text data exchange format
  • JSON is independent of language: JSON uses avascript syntax to describe data objects, but JSON is still independent of language and platform. The JSON parser and JSON library support many different programming languages. At present, many dynamic (PHP, JSP, .NET) programming languages ​​support JSON.

        The above explained the question of what JSON is from the three aspects of JSON concept, usage, and support platform. Now let me talk about my personal understanding.

  • JSON is a data representation method. JSON is not a specific value, but only a format for displaying values.
  • JSON is implemented in various languages, that is, it can form this display format.

2. What is a JSON object? What is JSON data? What is the JSON data string?

  • A JSON object is a representative of JSON data, just like a name represents a person. For example: Variables and values ​​are two different things, and they are used to represent values.
  • JSON data is data organized in the JSON format, and is the value represented by the JSON object.
  • The JSON data string is a string formed by escaping JSON data.

3. Use of cJSON basic functions. JSON data format check tool: https://www.sojson.com/

    //以下列JSON数据的合成和分解为例。
    {
        "name": "李四",
        "age": 25,
        "height": 173.56,
        "course": ["Chinese", "English", "math", "physics"],
        "scores": {
        "Chinese": 89,
        "English": 130,
        "math": 100,
        "physics": 90
        }
    }

#include <stdio.h>
#include "cJSON.h"

int main()
{     char json_str[1024] = {0};     printf_s("---->---->---->---->JSON data synthesis---->---- >---->---->\n");     //Add key-value pair function cJSON_AddItemToArray() Parameter 1 is the target object, parameter 2 is the key name, and parameter 3 is the key value.     //Create a JSON object and allocate a memory block to store JSON data.     cJSON* root = cJSON_CreateObject();     //Add key-value pairs to the JSON object, the key-value is a string.     cJSON_AddStringToObject(root, "name", "李四");     //Add key-value pairs to the JSON object, the key-value is a number.     cJSON_AddNumberToObject(root, "age", 25);     //Add key-value pairs to the JSON object, the key-value is a number.     cJSON_AddNumberToObject(root, "height", 173.56);










    //Add key-value pairs to the JSON object, the key-value is an array.
    cJSON *course = cJSON_CreateArray();
    //Add members to the array. Because parameter 2 is of cJON* type, it needs to be created by cJSON_CreateString.
    cJSON_AddItemToArray(course, cJSON_CreateString("Chinese"));
    cJSON_AddItemToArray(course, cJSON_CreateString("English"));
    cJSON_AddItemToArray(course, cJSON_CreateString("math"));
    cJSON_AddItemToArray(course, cJSON_CreateString("physics"));
    // Add the array course to the root object.
    cJSON_AddItemToObject(root, "course", course);
    
    cJSON* scores = cJSON_CreateObject();
    cJSON_AddItemToObject(scores, "Chinese", cJSON_CreateNumber(89));
    cJSON_AddItemToObject(scores, "English", cJSON_CreateNumber(130));
    cJSON_AddItemToObject(scores, "math", cJSON_CreateNumber(100));
    cJSON_AddItemToObject(scores, "physics", cJSON_CreateNumber(90));
    //Add scores to the root object.
    cJSON_AddItemToObject(root, "scores", scores);
    
    //JSON data is organized and printed out in unformatted format for testing. Additional memory will be requested.
    const char *cjson_unformat_str = cJSON_PrintUnformatted(root);

    //Format and print out for testing, and additional memory will be applied.
    const char* cjson_format_str = cJSON_Print(root);

    //cJSON_PrintBuffered(root, 1024, 1);
    //Destroy the JSON object and release the memory block occupied by the JSON data.
    cJSON_Delete(root);
    root = NULL;

    //cJSON_PrintUnformatted cJSON_Print will apply for memory, so it must be released after use.
    printf_s("----> cjson_unformat_str: %s\n\n", cjson_unformat_str);
    printf_s("----> cjson_format_str: %s\n", cjson_format_str);

    //Keep a copy.
    strcpy(json_str, cjson_unformat_str);
    free(cjson_unformat_str);
    cjson_unformat_str = NULL;

    free(cjson_format_str);
    cjson_format_str = NULL;

    printf_s("---->---->---->---->JSON data decomposition---->---->---->---->\n") ;
    //Parse the JSON string into a JSON object.
    root = cJSON_Parse(json_str);
    if (NULL == root) {         printf_s("----> cJSON_Parse failed\n");         return -1;     }


    //获取name键值的cJSON对象。
    cJSON* name = cJSON_GetObjectItem(root, "name");
    cJSON* age = cJSON_GetObjectItem(root, "age");
    cJSON* height = cJSON_GetObjectItem(root, "height");
    printf("----> name: %s\n", name->valuestring);
    printf("----> age: %d\n", age->valueint);
    printf("----> height: %lf\n", height->valuedouble);

    //Get the cJSON object of the course key value.
    course = cJSON_GetObjectItem(root, "course");
    //Get the size of the array
    int array_size = cJSON_GetArraySize(course);
    //Get the cJSON object of the array element in a loop
    cJSON* item = NULL;
    for (int i = 0; i <array_size; i++) {         item = cJSON_GetArrayItem(course, i);         printf("----> Array[%d]: %s\n", i, item->valuestring);     }     //Get the cJSON object of scores key value .     scores = cJSON_GetObjectItem(root, "scores");     cJSON* Chinese = cJSON_GetObjectItem(scores, "Chinese");     cJSON* English = cJSON_GetObjectItem(scores, "English");     cJSON* math = cJSON_GetObjectItem(scores, "math");



    





    cJSON* physics = cJSON_GetObjectItem(scores, "physics");
    printf_s("----> Chinese: %d\n", Chinese->valueint);
    printf_s("----> English: %d\n", English->valueint);
    printf_s("----> math: %d\n", math->valueint);
    printf_s("----> physics: %d\n", physics->valueint);
    cJSON_Delete(root);
    root = NULL;

    while (1)
        ;
}

Guess you like

Origin blog.csdn.net/qq_34473570/article/details/108655409