cJSON中主要的数据结构是cJSON结构体:
/* The cJSON structure: */
// cJSON对象
typedef struct cJSON {
// 下一个、上一个json对象
struct cJSON *next,*prev; /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
// 字节点
struct cJSON *child; /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
// 对象类型:整形、字符串、浮点、布尔、对象、数组等
int type; /* The type of the item, as above. */
// 不同的类型对应的值
char *valuestring; /* The item's string, if type==cJSON_String */
int valueint; /* The item's number, if type==cJSON_Number */
double valuedouble; /* The item's number, if type==cJSON_Number */
// 节点的名字
char *string; /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
} cJSON;
json节点的类型有:
/* cJSON Types: */
// json节点的类型
// 布尔true/false
#define cJSON_False 0
#define cJSON_True 1
// null
#define cJSON_NULL 2
// 数值整形
#define cJSON_Number 3
// 字符串
#define cJSON_String 4
// 数组
#define cJSON_Array 5
// 对象
#define cJSON_Object 6
// 节点是否被引用
#define cJSON_IsReference 256
// json字符串是否为const
#define cJSON_StringIsConst 512
内存分配与回收的钩子(默认是系统的malloc和free):
// 节点的分配/回收函数(钩子)
typedef struct cJSON_Hooks {
void *(*malloc_fn)(size_t sz);
void (*free_fn)(void *ptr);
} cJSON_Hooks;