1.资源
2.编译
如下几个文件是cjson
的核心文件:
cJSON.c
cJSON.h
cJSON_Utils.c
cJSON_Utils.h
其中,可以使用文件:
test.c
作为基础的案例代码,编译与执行方法如下:
$ gcc cJSON.c test.c cJSON_Utils.c
$ ./a.out
Version: 1.7.15
------------test 1----------------------
{
"name": "Jack (\"Bee\") Nimble",
"format": {
"type": "rect",
"width": 1920,
"height": 1080,
"interlace": false,
"frame rate": 24
}
}
------------test 2----------------------
["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
------------test 3----------------------
[[0, -1, 0], [1, 0, 0], [0, 0, 1]]
------------test 4----------------------
{
"Image": {
"Width": 800,
"Height": 600,
"Title": "View from 15th Floor",
"Thumbnail": {
"Url": "http:/*www.example.com/image/481989943",
"Height": 125,
"Width": "100"
},
"IDs": [116, 943, 234, 38793]
}
}
------------test 5----------------------
[{
"precision": "zip",
"Latitude": 37.7668,
"Longitude": -122.3959,
"Address": "",
"City": "SAN FRANCISCO",
"State": "CA",
"Zip": "94107",
"Country": "US"
}, {
"precision": "zip",
"Latitude": 37.371991,
"Longitude": -122.026,
"Address": "",
"City": "SUNNYVALE",
"State": "CA",
"Zip": "94085",
"Country": "US"
}]
------------test 6----------------------
{
"number": null
}
3.使用
3.1 mqtt应用
3.1.1 设备登录主题(设备上线发送一次)
基于 url
: iot/{product_uuid}/{sn}/login
,上行:
{
"sn": "1010", //设备sn
"version": "2.1.3" //嵌入式版本
}
可以使用如下代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"
/* Create a bunch of objects as demonstration. */
static int print_preallocated(cJSON *root)
{
/* declarations */
char *out = NULL;
char *buf = NULL;
size_t len = 0;
/* formatted print */
out = cJSON_Print(root);
/* create buffer to succeed */
/* the extra 5 bytes are because of inaccuracies when reserving memory */
len = strlen(out) + 5;
buf = (char*)malloc(len);
if (buf == NULL)
{
printf("Failed to allocate memory.\n");
exit(1);
}
/* Print to buffer */
if (!cJSON_PrintPreallocated(root, buf, (int)len, 1)) {
printf("cJSON_PrintPreallocated failed!\n");
if (strcmp(out, buf) != 0) {
printf("cJSON_PrintPreallocated not the same as cJSON_Print!\n");
printf("cJSON_Print result:\n%s\n", out);
printf("cJSON_PrintPreallocated result:\n%s\n", buf);
}
free(out);
free(buf_fail);
free(buf);
return -1;
}
/* success */
printf("%s\n", buf);
free(out);
free(buf);
return 0;
}
static void create_objects(void)
{
cJSON *root = NULL;
root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "sn", "1010");
cJSON_AddStringToObject(root,"version","2.1.3");
/* Print to text */
if (print_preallocated(root) != 0) {
cJSON_Delete(root);
exit(EXIT_FAILURE);
}
cJSON_Delete(root);
}
int main(void)
{
/* print the version */
printf("Version: %s\n", cJSON_Version());
/* Now some samplecode for building objects concisely: */
create_objects();
return 0;
}
执行命令:
$ ./a.out
Version: 1.7.15
{
"sn": "1010",
"version": "2.1.3"
}
3.1.2 设备登录主题(设备下发远程升级命令)
基于 url
: iot/{product_uuid}/{sn}/login
:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"
/* Create a bunch of objects as demonstration. */
static int print_preallocated(cJSON *root)
{
/* declarations */
char *out = NULL;
char *buf = NULL;
size_t len = 0;
/* formatted print */
out = cJSON_Print(root);
/* create buffer to succeed */
/* the extra 5 bytes are because of inaccuracies when reserving memory */
len = strlen(out) + 5;
buf = (char*)malloc(len);
if (buf == NULL)
{
printf("Failed to allocate memory.\n");
exit(1);
}
/* Print to buffer */
if (!cJSON_PrintPreallocated(root, buf, (int)len, 1)) {
printf("cJSON_PrintPreallocated failed!\n");
if (strcmp(out, buf) != 0) {
printf("cJSON_PrintPreallocated not the same as cJSON_Print!\n");
printf("cJSON_Print result:\n%s\n", out);
printf("cJSON_PrintPreallocated result:\n%s\n", buf);
}
free(out);
free(buf);
return -1;
}
/* success */
printf("%s\n", buf);
free(out);
free(buf);
return 0;
}
static void create_objects(void)
{
cJSON *root = NULL;
root = cJSON_CreateObject();
cJSON_AddNumberToObject(root,"size",13428);
cJSON_AddStringToObject(root, "url", "https://www.baidu.com");
cJSON_AddStringToObject(root,"version","3.7.0");
/* Print to text */
if (print_preallocated(root) != 0) {
cJSON_Delete(root);
exit(EXIT_FAILURE);
}
cJSON_Delete(root);
}
int main(void)
{
/* print the version */
printf("Version: %s\n", cJSON_Version());
/* Now some samplecode for building objects concisely: */
create_objects();
return 0;
}
执行结果如下:
3.1.3 解析数据
可以参看如下代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cJSON.h"
/* Create a bunch of objects as demonstration. */
static int print_preallocated(cJSON *root)
{
/* declarations */
char *out = NULL;
char *buf = NULL;
size_t len = 0;
/* formatted print */
out = cJSON_Print(root);
/* create buffer to succeed */
/* the extra 5 bytes are because of inaccuracies when reserving memory */
len = strlen(out) + 5;
buf = (char*)malloc(len);
if (buf == NULL)
{
printf("Failed to allocate memory.\n");
exit(1);
}
/* Print to buffer */
if (!cJSON_PrintPreallocated(root, buf, (int)len, 1)) {
printf("cJSON_PrintPreallocated failed!\n");
if (strcmp(out, buf) != 0) {
printf("cJSON_PrintPreallocated not the same as cJSON_Print!\n");
printf("cJSON_Print result:\n%s\n", out);
printf("cJSON_PrintPreallocated result:\n%s\n", buf);
}
free(out);
free(buf);
return -1;
}
/* success */
printf("%s\n", buf);
free(out);
free(buf);
return 0;
}
static cJSON * prase_json(void)
{
char *string = "{ \"size\": 10, \"name\": \"zhangsan\"}";
cJSON *parsed = NULL;
parsed = cJSON_Parse(string);
return parsed;
}
int main(void)
{
static cJSON *cjson_value = NULL;
/* print the version */
printf("Version: %s\n", cJSON_Version());
/* Now some samplecode for building objects concisely: */
cjson_value = prase_json();
if (print_preallocated(cjson_value) != 0) {
cJSON_Delete(cjson_value);
return 0;
}
cJSON_Delete(cjson_value);
return 0;
}
执行结果如下: