C language source code parsing JSON

2020-01-09

Keywords: cJSON, linux JSON parsing


 

JSON is a very commonly used in the Internet field of lightweight data exchange protocol.

 

It almost XML status, but in terms of the author, I prefer JSON style, because it is more in line with our habits of thinking, the same copy of the data, JSON format than XML is to clear a number.

 

Recently I need to parse JSON format in the C language, the Internet looking for a meal, to find a very good open-source code. After a burst of research and subsequent amendments, so I finally became a very handy with the C language version of the JSON parser.

 

The author now been little modified code record it, to want to give yourself a make a note, and secondly, I hope to help students have the same needs.

 

The source code and a demo is easy to blog packed park network disk:

https://files.cnblogs.com/files/chorm590/C%E8%AF%AD%E8%A8%80%E8%A7%A3%E6%9E%90JSON%E7%A4%BA%E4%BE%8B.zip

 

This compressed file contains two copies of the source code:

1, cJSON parser and the original version of the example described readme

2, modified by the author Sample Code

 

Here only the author modified the code to briefly explain.

 

Modified code file by the author there are three code file, as shown below:

 

Which demo.c Demonstration Program, prepared a total of three common JSON data format and its analytical way to showcase cJSON usage. The entire sample program is very simple, the students will be able to read a little comprehend its usage.

 

In this parser, JSON all nodes are abstracted into a cJSON objects, i.e. cJSON structure:

 

 

As shown above, in this parser, JSON JSON array node and target node tied next, prev, child pointer three variables. Whenever involves parsing JSON JSON array of objects, which can be quickly used to find the corresponding pointer variable three values.

 

Type were recorded in the node type of variable. Alternatively the value of the type shown below:

 

 

Value nodes were stored in valuestring, valueint, valuedouble in. Where the boolean value is stored as valueint. 1 represents true, false represents 0.

 

The name of the node itself, is stored in the string variable struct cJSON structure shown in FIG.

 

As for the last pure_json variable, it is the author of his own to add. Its purpose is to add a save after removing the extra space characters and line feed characters in plain JSON text. Yes, only the default cJSON parser parse JSON text format most standard case, as shown below:

 

And can not be resolved after the layout JSON format as shown below:

 

 

"A little bit modified version of" I mentioned earlier refers to parse JSON text can be added after the publishing of this function. We order after the text after the "Format" can easily run out of free fall, add this pur_string variable cJSON structure. At the same time, the author of this format can also leave space characters and line feed characters in the string value.

 

When using this JSON parser, just to the original JSON string passed cJSON_Parse () function will automatically parse the entire string again, and creates a corresponding node list.

 

It must be noted, however, due to the parsing JSON is to use malloc to allocate memory space, so after you are finished using them must be freed memory. Release memory way too simple, straightforward root node as an argument cJSON_Delete () function can be.

 

As for the parser process, interested students can go tracking cJSON.c source code implementation. It is not complicated, I will not go into here.

 

If your time is limited, you can also directly refer to or modify the sample code to the author directly.

 

Finally, paste the author's demo code:

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

static void parse_normal_json();
static void parse_object_json();
static void parse_array_json();

int main()
{
    printf("hello world\n");
    
    parse_normal_json();
    parse_object_json();
    parse_array_json();
    
    return 0;
}

static void parse_normal_json()
{
    printf("\n\n\n>>> parse_normal_json()\n\n");
    const char *json = "\n\
    {\n\
        \"key0\":\"Js   on is a fun\nny data for      mat!\",\n\
        \"key1\":\"value1\",\n\
        \"key2\":26,\n\
        \"key3\":false\n\
    }\n\
    ";
    
    printf("json:\n%s\n\n", json);
    
    cJSON *root = cJSON_Parse(json);
    if(root == 0)
    {
        printf("error\n");
        return;
    }
    
    cJSON *key0_node = cJSON_GetObjectItem(root, "key0");
    if(key0_node == 0)
        return;
    printf("key0 name:\n\t%s\nkey0 value:\n\t%s\n", key0_node->string, key0_node->valuestring);
    
    cJSON *key1_node = cJSON_GetObjectItem(root, "key1");
    if(key1_node == 0)
        return;
    printf("key1 value:\n\t%s\n", key1_node->valuestring);
    
    cJSON *key2_node = cJSON_GetObjectItem(root, "key2");
    if(key2_node == 0)
        return;
    printf("key2 value:\n\t%d\n", key2_node->valueint);
    
    cJSON *key3_node = cJSON_GetObjectItem(root, "key3");
    if(key3_node == 0)
        return;
    printf("key3 value:\n\t%d\n", key1_node->valueint);
    
    cJSON_Delete(root);
}

static void parse_object_json()
{
    printf("\n\n\n>>> parse_object_json()\n\n");
    const char *json = "\n\
    {\n\
        \"obj\":{\n\
            \"key\":71,\n\
            \"name\":22\n\
        }\n\
    }\n\
    ";
    
    printf("json:\n%s\n\n", json);
    
    cJSON *root = cJSON_Parse(json);
    if(root == 0)
    {
        printf("error\n");
        return;
    }
    
    cJSON *obj_node = cJSON_GetObjectItem(root, "obj");
    if(obj_node == 0)
        return;
    
    cJSON *key_node = cJSON_GetObjectItem(obj_node, "key");
    if(key_node == 0)
        return;
    printf("key value:\n\t%d\n", key_node->valueint);
    
    cJSON *child = obj_node->child;
    if(child == 0)
        return;
    
    printf("key name:\n\t%s\nkey value:\n\t%d\n", child->string, child->valueint);
    cJSON *name_node = child->next;
    if(name_node == 0)
        return;
    printf("name name:\n\t%s\nname value:\n\t%d\n", name_node->string, name_node->valueint);
    
    cJSON_Delete(root);
}

static void parse_array_json()
{
     printf("\n\n\n>>> parse_array_json()\n\n");
    const char *json = "\n\
    {\n\
        \"arrs\":[15,3,99,108,22]\n\
    }\n\
    ";
    
    printf("json:\n%s\n\n", json);
    
    cJSON *root = cJSON_Parse(json);
    if(root == 0)
    {
        printf("error\n");
        return;
    }
    
    cJSON *arrs_node = cJSON_GetObjectItem(root, "arrs");
    if(arrs_node == 0)
        return;
    
    if(arrs_node->type == cJSON_Array)
    {
        cJSON *node = arrs_node->child;
        int i = 0;
        while(node != 0)
        {
            printf("item%d:\n\t%d\n", i++, node->valueint);
            node = node->next;
        }
    }
    else
    {
        printf("arrs_node is not a json_array.\n");
    }
    
    
    cJSON_Delete(root);
}
cJSON的演示代码

 


 

Guess you like

Origin www.cnblogs.com/chorm590/p/12152467.html