MCU C language JSON data processing

Posted by Ehailey on Thu, 17 Feb 2022 07:37:53 +0100

1, JSON development ideas

Recently, there is a project that uses JSON data to communicate with the host computer. I will share the development process with you. At first, I wanted to use the JSON library provided by MDK, and there was no problem in compiling, but finally I found that the library had requirements for the compiler, which was not very convenient. Finally, I gave up this idea and wrote function functions with JSON C code downloaded from the Internet. Finally, all functions were realized. Therefore, I strongly recommend that you adopt this development idea. The code is written in C language, which is invisible.

2, My understanding of JSON code

2.1 format of JSON

{
    "CMD":    4,
    "DeviceType":    1,
    "Control":    [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
}


First, the previous code. This is the basic format of JSON code. Include a JSON code in curly braces. The code package includes many nodes. The node types include INT type, string type and double type. These are three basic types. In addition to the basic type, it can be JSON node and matrix node. This is a bit similar to the data types of C language. There are not only basic types, but also composite types.

Next, let's look at the data format of JSON:

/* The cJSON structure: */
typedef struct cJSON
{
    /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
    struct cJSON *next;
    struct cJSON *prev;
    /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
    struct cJSON *child;

    /* The type of the item, as above. */
    int type;

    /* The item's string, if type==cJSON_String  and type == cJSON_Raw */
    char *valuestring;
    /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
    int valueint;
    /* The item's number, if type==cJSON_Number */
    double valuedouble;

    /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
    char *string;
} cJSON;

Seeing this, you can basically understand what JSON is about. First, his name is char *string; Defined content.

Its type is defined by int type, and then there are three basic data types. In addition to the basic data types, they can also be combined data types, including complete JSON structure and matrix data types, which are similar to the basic data types and combined data types of C language. It seems that many things are figured out.

2.2 how to use

This part discusses this problem from two perspectives: how to parse and how to package. For each function implementation, the following is a detailed analysis

2.2.1 how to create a JSON

cJSON *json_p_rd, *tmp1,*tmp2,*tmp3,*tmp4,*tmp5,*tmp6,*tmp7;
tmp3=cJSON_CreateObject();	
json_p_rd=cJSON_Parse(text);

From the above code, you can see how to create and parse a JSON data structure. Where text is a string of JSON data.

2.2.2 how to add JSON nodes

cJSON_AddNumberToObject(tmp3,"CMD",1);
cJSON_AddStringToObject(tmp3, "OperateString", "The Cmd Error !");
cJSON_AddItemToObject(tmp6,"OperateValue", tmp7=cJSON_CreateArray( ));	

You can add nodes through the above functions

2.2.3 how to obtain a node

 node = cJSON_GetObjectItem(json_p_rd,"Control");	
 if((tmp3 !=NULL) && (tmp->type==cJSON_Number))
 {
    dat1=tmp3;

}

The above code can get a node.

Topics: C JSON Single-Chip Microcomputer Software development