参考资料:cJson数组的生成与解析

JSON规则

键值对 “key”:”value”

  • json是一个键值对集合
  • 以”{“开始,以”}”结束,允许嵌套使用
  • 每个名称和值成对出现,名称和值之间使用”:”分隔
  • 键值对之间用”,”分隔

value取值

  • 可以是一个新的json对象
  • 数组:使用”[“和”]”表示
  • 数字:直接表示,可以是整数,也可以是浮点数
  • 字符串:使用引号”string”表示
  • 字面值:false、null、true中的一个(必须是小写)

字符串JSON互转

cJSON *json = NULL;
char *str = NULL;
json = cJSON_Parse(strMsg);
str = cJSON_PrintUnformatted(rulesListJson);
cJSON_Delete(json);
free(str);

数字、字符串

{"number":23,"name":"xiaoqiang"}

构造

char *str = NULL;
cJSON *json = NULL;
sendJson = cJSON_CreateObject();
cJSON_AddNumberToObject(json, "number", 23);
cJSON_AddStringToObject(json, "name", "xiaoqiang");
str = cJSON_PrintUnformatted(json);

解析

json = cJSON_Parse(strMsg);
cJSON *number = cJSON_GetObjectItem(json, "number");
printf("%d\n", number->valueint);
cJSON *name = cJSON_GetObjectItem(json, "name");
printf("%d\n", name->valuestring);

数组

{"array":["hello","world"]}

构造

cJSON *json = cJSON_CreateObject();
cJSON *array = NULL;
cJSON_AddItemToObject(json, "array", array= cJSON_CreateArray());

解析

size_t jsonArraySize = 0;
cJSON *recvSubItem = NULL;
jsonArraySize = cJSON_GetArraySize(recvListJson);
while (jsonArraySize--)
{
recvSubItem = cJSON_GetArrayItem(recvListJson, jsonArraySize);
}

实例

构造

{"name":"kaikai","hobby":[{"fruit":"apple"},{"book":"simple love"}],"reading":{"simple":"love"},"ts":1234567891011}
#include <stdio.h>
#include <stdlib.h>

#include "cJSON.h"

#define IS_NULL(fmt, ...) \
if (fmt == NULL) \
{ \
return -1; \
}

int main(int argc, char *argv[])
{
char *str = NULL;
cJSON *rootJson = NULL;
cJSON *arrayJson = NULL;
cJSON *arrayItemJson = NULL;
cJSON *subItem = NULL;

// 1. 创建一个根节点
rootJson = cJSON_CreateObject();
IS_NULL(rootJson);

// 2. 向根节点添加一个 字符串 节点
cJSON_AddStringToObject(rootJson, "name", "kaikai");

// 3. 向根节点添加一个 数组 节点
cJSON_AddItemToObject(rootJson, "hobby", arrayItemJson = cJSON_CreateArray());
// 3.1 向数组里添加一个元素,并且该元素为json对象
cJSON_AddItemToArray(arrayItemJson, subItem = cJSON_CreateObject());
cJSON_AddStringToObject(subItem, "fruit", "apple");
cJSON_AddItemToArray(arrayItemJson, subItem = cJSON_CreateObject());
cJSON_AddStringToObject(subItem, "book", "simple love");

// 4. 向根节点添加一个 对象 节点
cJSON_AddItemToObject(rootJson, "reading", subItem = cJSON_CreateObject());
cJSON_AddStringToObject(subItem, "simple", "love");

// 5. 向根节点添加一个 数字 节点
cJSON_AddNumberToObject(rootJson, "ts", 1234567891011);

// 6. 将json输出为字符串
str = cJSON_PrintUnformatted(rootJson);

// 7. 释放内存
cJSON_Delete(rootJson);
if (NULL != str)
{
printf("%s\n", str);
free(str);
str = NULL;
}
return 0;
}

解析

#include <stdio.h>
#include <stdlib.h>

#include "cJSON.h"

#define IS_NULL(fmt, ...) \
if (fmt == NULL) \
{ \
return -1; \
}

#define JSON_STR(fmt, ...) \
if (fmt && fmt->valuestring) \
{ \
printf("%s\n", fmt->valuestring); \
}
char str[] = "{\"name\":\"kaikai\",\"hobby\":[{\"fruit\":\"apple\"},{\"book\":\"simple love\"}],\"reading\":{\"simple\":\"love\"},\"ts\":1234567891011}";
int main(int argc, char *argv[])
{
char *p = NULL;
size_t arraySize = 0;
cJSON *rootJson = NULL;
cJSON *objItemJson = NULL;
cJSON *arrayItemJson = NULL;
cJSON *subItem = NULL;
printf("%s\n", str);

rootJson = cJSON_Parse(str);
IS_NULL(rootJson);

objItemJson = cJSON_GetObjectItem(rootJson, "name");
JSON_STR(objItemJson);

objItemJson = cJSON_GetObjectItem(rootJson, "hobby");
if (cJSON_IsArray(objItemJson))
{
arraySize = cJSON_GetArraySize(objItemJson);
while (arraySize--)
{
arrayItemJson = cJSON_GetArrayItem(objItemJson, arraySize);
subItem = cJSON_GetObjectItem(arrayItemJson, "book");
JSON_STR(subItem);
subItem = cJSON_GetObjectItem(arrayItemJson, "fruit");
JSON_STR(subItem);
}
}

objItemJson = cJSON_GetObjectItem(rootJson, "reading");
subItem = cJSON_GetObjectItem(objItemJson, "simple");
JSON_STR(subItem);

cJSON_Delete(rootJson);

return 0;
}