/* cJSON Types: */#definecJSON_Invalid(0)#definecJSON_False(1<<0)#definecJSON_True(1<<1)#definecJSON_NULL(1<<2)#definecJSON_Number(1<<3)#definecJSON_String(1<<4)#definecJSON_Array(1<<5)#definecJSON_Object(1<<6)#definecJSON_Raw(1<<7)/* raw json */
cJSON布尔值
typedefint cJSON_bool;
cJSON结构体
/* The cJSON structure: */typedefstructcJSON{/* 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. */int type;/* 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;
String:键值对的key
type:值的类型
valuestring:指向字符串或cJSON_Raw
valueint:储存整数
valuedouble:储存浮点数
child:指向子对象或数组
判断节点类型
/* 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);
序列化
创建cJSON项,返回节点地址
/* These calls create a cJSON item of the appropriate type. */CJSON_PUBLIC(cJSON *)cJSON_CreateNull(void);CJSON_PUBLIC(cJSON *)cJSON_CreateTrue(void);CJSON_PUBLIC(cJSON *)cJSON_CreateFalse(void);CJSON_PUBLIC(cJSON *)cJSON_CreateBool(cJSON_bool boolean);CJSON_PUBLIC(cJSON *)cJSON_CreateNumber(double num);CJSON_PUBLIC(cJSON *)cJSON_CreateString(constchar*string);/* raw json */CJSON_PUBLIC(cJSON *)cJSON_CreateRaw(constchar*raw);CJSON_PUBLIC(cJSON *)cJSON_CreateArray(void);CJSON_PUBLIC(cJSON *)cJSON_CreateObject(void);
添加项至对象或数组
/* Append item to the specified array/object. */CJSON_PUBLIC(cJSON_bool)cJSON_AddItemToArray(cJSON *array, cJSON *item);CJSON_PUBLIC(cJSON_bool)cJSON_AddItemToObject(cJSON *object,constchar*string, cJSON *item);
同时创建、添加项至对象
/* Helper functions for creating and adding items to an object at the same time.
* They return the added item or NULL on failure. */CJSON_PUBLIC(cJSON*)cJSON_AddNullToObject(cJSON *const object,constchar*const name);CJSON_PUBLIC(cJSON*)cJSON_AddTrueToObject(cJSON *const object,constchar*const name);CJSON_PUBLIC(cJSON*)cJSON_AddFalseToObject(cJSON *const object,constchar*const name);CJSON_PUBLIC(cJSON*)cJSON_AddBoolToObject(cJSON *const object,constchar*const name,const cJSON_bool boolean);CJSON_PUBLIC(cJSON*)cJSON_AddNumberToObject(cJSON *const object,constchar*const name,constdouble number);CJSON_PUBLIC(cJSON*)cJSON_AddStringToObject(cJSON *const object,constchar*const name,constchar*const string);CJSON_PUBLIC(cJSON*)cJSON_AddRawToObject(cJSON *const object,constchar*const name,constchar*const raw);CJSON_PUBLIC(cJSON*)cJSON_AddObjectToObject(cJSON *const object,constchar*const name);CJSON_PUBLIC(cJSON*)cJSON_AddArrayToObject(cJSON *const object,constchar*const name);
/* Render a cJSON entity to text for transfer/storage. */CJSON_PUBLIC(char*)cJSON_Print(const cJSON *item);/* Render a cJSON entity to text for transfer/storage without any formatting. */CJSON_PUBLIC(char*)cJSON_PrintUnformatted(const cJSON *item);
反序列化
字符串转cJSON链表
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */CJSON_PUBLIC(cJSON *)cJSON_Parse(constchar*value);CJSON_PUBLIC(cJSON *)cJSON_ParseWithLength(constchar*value,size_t buffer_length);
由key取出对象中的项
/* Get item "string" from object. Case insensitive. */CJSON_PUBLIC(cJSON *)cJSON_GetObjectItem(const cJSON *const object,constchar*const string);
遍历数组中的项
/* Returns the number of items in an array (or object). */CJSON_PUBLIC(int)cJSON_GetArraySize(const cJSON *array);/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */CJSON_PUBLIC(cJSON *)cJSON_GetArrayItem(const cJSON *array,int index);
释放cJSON链表
/* Delete a cJSON entity and all subentities. */CJSON_PUBLIC(void)cJSON_Delete(cJSON *item);