This post was last edited by ID.LODA on 2018-12-21 09:19 The use of JSON strings in IoT devices is also quite common nowadays. Let’s share and learn about it today. The cJSON library can be downloaded from the KEIL pack package. After downloading, I put it in the SDK_2.4.2_MIMXRT1052xxxxB\middleware directory. The open source protocols are all here, which is convenient for searching.
Add the corresponding source files in the project and add the cJSON header file directory to the settings. CSJON will use dynamic memory allocation. You can use the management function you wrote in the previous section and initialize it as follows
It is also okay not to set it, because the library has a default memory allocation function, which calls the compiler's malloc and free
The following is a general JSON usage process, which initializes the memory allocation function, creates one or more JSON structures, adds strings or numbers or variables of different types, or sub-JSON variables, and then generates a JSON string. After using this string, release the JSON memory, otherwise it will always be occupied.
With the previous memory management experiment, you can clearly see the memory usage and better understand the memory allocation mechanism of cJSON
cJSON_Print generates the corresponding json array and you can see that it occupies memory separately, so you can release the corresponding root and child json memory and print the output
The JSON parsing process is similar. First create a json variable, convert the data into a json structure, and then get the corresponding object json and take the value.
Objectjson needs to correspond to the root cjson to read the value correctly. The following SystemCoreClock's object json belongs to child-json, but you will fail to get it from root-json.
After the correct correspondence, you can get the value
After calling the json array, you need to release it, otherwise it will accumulate calls for a long time and cause memory overflow
The above is the generation and parsing of json, attach the project
hello_eeworld.rar
(148.9 KB, downloads: 12)
, put it in the SDK_2.4.2_MIMXRT1052xxxxB\boards\EasyARM-RT1052-V1_0\demo_apps directory and unzip it. The keil version is V5.26.2.0 This content is originally created by EEWORLD forum user ID.LODA. If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source.