cJSON函数用法
- /* 以字符串形式返回cJSON的版本 /
CJSON_PUBLIC(const char) cJSON_Version(void); - /* 向cJSON提供malloc, realloc和free函数 /
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks hooks); - /* 提供一个JSON块,这将返回一个您可以查询的cJSON对象。*/
CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value,
size_t buffer_length);
cJSON_Print()函数 和 CJSON_Parse()函数的作用:
前者是 JSON 的数据格式转换为 JSON 字符串,用于 JSON 组装,
后者是 JSON 字符串转换为 JSON 的数据格式,用于 JSON 解析
-
/* 内存管理:调用者总是负责从所有变量的cJSON_Parse(使用cJSON_Delete)和
cJSON_Print(使用stdlib freallocated)中释放结果,调用者对缓冲区有完全的责任。*/
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value); -
/* 将cJSON实体呈现为用于传输/存储的文本。 */
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item); -
/* 将cJSON实体呈现为文本,用于传输/存储,不需要任何格式化。*/
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item); -
/* 使用缓冲策略将cJSON实体渲染为文本。预缓冲是对最终大小的猜测。
猜中会减少再分配。Fmt =0表示未格式化,=1表示已格式化 */
CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item,
int prebuffer,
cJSON_bool fmt); -
/* 使用内存中已分配的具有给定长度的缓冲区将cJSON实体渲染为文本。
成功时返回1,失败时返回0。/
/ 注意:cJSON在估计它将使用多少内存时并不总是100%准确,
所以为了安全,要比实际需要多分配5个字节 */
CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item,
char *buffer,
const int length,
const cJSON_bool format); -
/* 删除一个cJSON实体和所有子实体。 */
CJSON_PUBLIC(void) cJSON_Delete(cJSON *item); -
/* 返回数组(或对象)中的项数。*/
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array); -
/* 从数组"array"中检索项目号"index"。如果不成功,返回NULL。*/
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index); -
/* 检查项目类型并返回其值 */
CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item);
CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const 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项 */
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(const char *string);
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char raw); / raw json */
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void); -
/* 在valuestring引用字符串的地方创建一个字符串,这样它就不会被cJSON_Delete释放 */
CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string); -
/* 创建一个对象/数组,只引用它的元素,这样它们就不会被cJSON_Delete释放 */
CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child);
CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child); -
/* 这些实用程序创建计数项的数组。参数计数不能大于数字数组中的元素数量,否则数组访问将超出限制。 */
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(const int *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(const float *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(const double *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(const char *const *strings, int count); -
/* 将项附加到指定的数组/对象。 */
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object,
const char *string,
cJSON *item); -
/* 从数组/对象中删除/分离项。 */
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item); //via:通过
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string); -
/* 更新数组项。 */
CJSON_PUBLIC(cJSON_bool) cJSON_InsertItemInArray(cJSON *array, int which, cJSON newitem); / Shifts pre-existing items to the right. */
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement);
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem);
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);
CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem); -
/* 复制一个cJSON项 */
CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse); -
/* 缩小字符串,从字符串中删除空白字符(如’ ‘,’\t’, ‘\r’, ‘\n’)。输入指针json不能指向只读地址区,比如字符串常量,但应该指向可读可写的地址区 */
CJSON_PUBLIC(void) cJSON_Minify(char *json); -
/* 同时向对象中创建和添加项的帮助函数。成功时返回添加的项,失败时返回NULL /
CJSON_PUBLIC(cJSON) cJSON_AddNullToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean);
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number);
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw);
CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name); -
编译命令
gcc *.c cJSON.c -lm