C语言:记录cjson生成带数组的json数据
·
cJSON* xjson = cJSON_CreateObject();
cJSON_AddItemToObject(xjson, "a", cJSON_CreateString("aaa"));
cJSON_AddItemToObject(xjson, "b", cJSON_CreateNumber(1));
cJSON *json_data = cJSON_CreateObject();
cJSON *a_item = cJSON_CreateArray();
cJSON_AddItemToObject(json_data,"c", cJSON_CreateNumber(2));
cJSON_AddItemToObject(json_data,"d", a_item);
cJSON_AddItemToArray(a_item, cJSON_CreateString("hello"));
cJSON_AddItemToArray(a_item, cJSON_CreateString("world"));
cJSON_AddItemToObject(xjson,"data", json_data);
char *buf = cJSON_Print(xjson);
printf("%s\n",buf );//{"a":"aaa","b":1,"data":{"c":1,"d":["hello","world"]}}
free(buf);
cJSON_Delete(xjson);
结果:
{
"a":"aaa",
"b":1,
"data":{
"c":1,
"d":[
"hello",
"world"
]
}
}
更多推荐
已为社区贡献6条内容
所有评论(0)