这是一条json
{"name":"hany","age":18}
一对{}
就是一个JSON object,
object里的"mane"
"age"
被称为key name,"hany"
18
被称为key value。
KEY name是一个字符串;
{"age":18}
{"height":180.1}
{"name":"hany"}
{"module":[]}
{"people":{"name":"hany"}}
使用C库函数strstr
判断有无关心的关键字即可。
#include <stdio.h> #include <string.h> const char *rpc_cmd = "{"id":5,"action":"led_on"}"; if (strstr(rpc_cmd, "led_on")) { printf("open led"); }
先写出这条json的格式,再使用C库函数snprintf
对其进行value值填充即可
const char *query_did_format = "{\"id\":%d,\"method\":\"local.query_did\", \"params\":\"\"}"; char rpc_buf[64] = {0}; snprintf(rpc_buf, sizeof(rpc_buf), query_did_format, rand());
static const char *monitor_without_hd = "{\n\ \t\t\"name\": \"lame monitor\",\n\ \t\t\"resolutions\":\t[{\n\ \t\t\t\"width\":\t640,\n\ \t\t\t\"height\":\t480\n\ \t\t}]\n\ }"; /*return 1 if the monitor supports full HD , 0 otherwise*/ static int supports_full_hd(const char * const monitor) { int status = 0; /*首先调用cJSON_Parse,解析JSON数据包。该函数会malloc出一块内存,使用完毕后要手动释放*/ cJSON *monitor_json = cJSON_Parse(monitor); if (monitor_json == NULL) { status = 0; goto end; } /*在JSON Object中,查找 key name 为 “name”的 key, 找到后返回其 key value */ const cJSON *name = cJSON_GetObjectItemCaseSensitive(monitor_json, "name"); /*判断 key value是不是字符串,并且字符串不为空*/ if (cJSON_IsString(name) && (name->valuestring != NULL)) { printf("checking monitor \"%s\", name->valuestring); } /*在JSON Object中,查找key name 为"resolution"的key,找到后返回其值*/ const cJSON *resolutions = cJSON_GetObjectItemCaseSensitive(monitor_json, "resolutions"); /*判断resolution是不是数组*/ if (!cJSON_IsArray(resolutions)) { goto end; } const cJSON *resolution = NULL; /*在resolutions数组中,遍历每一个JSON OBJECT */ cJSON_ArrayForEach(resolution, resolutions) { cJSON *width = cJSON_GetObjectItemCaseSensitive(resolution, "width"); cJSON *height = cJSON_GetObjectItemCaseSensitive(resolution, "height"); if (!cJSON_IsNumber(width) || !cJSON_IsNumber(height)) { status = 0; goto end; } if (width->valueint == 1920 && width->valueint == 1080) { status = 1; goto end; } } end: /*释放cJSON_Parse分配出来的内存*/ cJSON_Delete(monitor_json); return status; }
{ "name": "Awesome 4K", "resolutions": [ { "width": 1280, "height": 720 }, { "width": 1920, "height": 1080 }, { "width": 3840, "height": 2160 } ] }
{"name": "Awesome 4K","resolutions": [{"width": 1280,"height": 720},{"width": 1920, "height": 1080},{"width": 3840,"height": 2160}]}
//NOTE: Returns a heap allocated string, you are required to free it after use. void create_monitor_with_helpers(void) { const unsigned int resolution_numbers[3][2] = { {1280, 720}, {1920, 1080}, {3840, 2160} }; char *string = NULL; cJSON *resolutions = NULL; size_t index = 0; /*创建{}。cJSON_CreateObject执行后,会申请一块内存,使用完毕后,需要手动释放*/ cJSON *monitor = cJSON_CreateObject(); /*{"name": "Awesome 4K"}*/ if (cJSON_AddStringToObject(monitor, "name", "Awesome 4K") == NULL) { goto end; } /*{"name": "Awesome 4K","resolutions":[]}*/ resolutions = cJSON_AddArrayToObject(monitor, "resolutions"); if (resolutions == NULL) { goto end; } //对数组进行赋值 for (index = 0; index < (sizeof(resolution_numbers) / (2 * sizeof(int))); ++index) { //创建{} cJSON *resolution = cJSON_CreateObject(); //加入键值对width if (cJSON_AddNumberToObject(resolution, "width", resolution_numbers[index][0]) == NULL) { goto end; } //加入键值对height if (cJSON_AddNumberToObject(resolution, "height", resolution_numbers[index][1]) == NULL) { goto end; } //将当前OBJECT 加入到数组 cJSON_AddItemToArray(resolutions, resolution); } //对创建好的JSON进行格式化。cJSON_Print执行后会提请一块内存,使用完毕后,需要手动释放 string = cJSON_Print(monitor); if (string == NULL) { fprintf(stderr, "Failed to print monitor.\n"); } else { cJSON_free(string); } end: cJSON_Delete(monitor); }
cJSON_Parse
cJSON_CreateObject
要用 cJSON_Delete
释放
cJSON_Print
cJSON_PrintUnformatted
要用 cJSON_free
释放