//节点类型#define cJSON_Invalid (0)#define cJSON_False (1 << 0)#define cJSON_True (1 << 1)#define cJSON_NULL (1 << 2)#define cJSON_Number (1 << 3)#define cJSON_String (1 << 4)#define cJSON_Array (1 << 5)#define cJSON_Object (1 << 6)#define cJSON_Raw (1 << 7) /* raw json *///节点结构体定义typedef struct cJSON //表示一个json节点{/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */structcJSON*next; //节点的下一个节点structcJSON*prev; //节点的上一个节点/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */structcJSON*child; //节点的子节点/* The type of the item, as above. */inttype; //就是上面的节点类型/* The item's string, if type==cJSON_String and type == cJSON_Raw */ char *valuestring; /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */int valueint;/* The item's number, if type==cJSON_Number */ double valuedouble;/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */ char *string;} cJSON;
解码(parse)
/* Memory Management: the caller is always responsible to free the results from all variants of cJSON_Parse (with cJSON_Delete) and cJSON_Print (with stdlib free, cJSON_Hooks.free_fn, or cJSON_free as appropriate). The exception is cJSON_PrintPreallocated, where the caller has full responsibility of the buffer. */
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. *///把json字符串解析为节点树对象,即解码CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
编码(print)
/* Render a cJSON entity to text for transfer/storage. *///把节点对象输出成json字符串,有格式化CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);/* Render a cJSON entity to text for transfer/storage without any formatting. *///把节点变量输出成json字符串,不带格式化CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
//各种操作节点的函数CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);//创建原始字符串节点CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void); //创建节点数组CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void); //创建节点对象/* These calls create a cJSON item of the appropriate type. */CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void); //创建空节点CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void); //创建true节点CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void); //创建false节点CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean); //创建布尔节点CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num); //创建数值节点CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string); //创建字符串节点/* Append item to the specified array/object. *///把创建好的节点添加到节点树上CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item);CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object,const char *string, cJSON *item);/* These functions check the type of an item *///判断节点类型CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON *const item);CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON *const item);CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON *const item);CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON *const item);CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON *const item);CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON *const item);CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON *const item);CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON *const item);CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON *const item);CJSON_PUBLIC(cJSON_bool) cJSON_IsRaw(const cJSON *const item);/* Delete a cJSON entity and all subentities. */CJSON_PUBLIC(void) cJSON_Delete(cJSON *c); //删除节点