cJSON函数用法

  1. /* 以字符串形式返回cJSON的版本 /
    CJSON_PUBLIC(const char
    ) cJSON_Version(void);
  2. /* 向cJSON提供malloc, realloc和free函数 /
    CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks
    hooks);
  3. /* 提供一个JSON块,这将返回一个您可以查询的cJSON对象。*/
    CJSON_PUBLIC(cJSON *) cJSON_ParseWithLength(const char *value,
    size_t buffer_length);

cJSON_Print()函数 和 CJSON_Parse()函数的作用:
前者是 JSON 的数据格式转换为 JSON 字符串,用于 JSON 组装,
后者是 JSON 字符串转换为 JSON 的数据格式,用于 JSON 解析

  1. /* 内存管理:调用者总是负责从所有变量的cJSON_Parse(使用cJSON_Delete)和
    cJSON_Print(使用stdlib freallocated)中释放结果,调用者对缓冲区有完全的责任。*/
    CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);

  2. /* 将cJSON实体呈现为用于传输/存储的文本。 */
    CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);

  3. /* 将cJSON实体呈现为文本,用于传输/存储,不需要任何格式化。*/
    CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);

  4. /* 使用缓冲策略将cJSON实体渲染为文本。预缓冲是对最终大小的猜测。
    猜中会减少再分配。Fmt =0表示未格式化,=1表示已格式化 */
    CJSON_PUBLIC(char *) cJSON_PrintBuffered(const cJSON *item,
    int prebuffer,
    cJSON_bool fmt);

  5. /* 使用内存中已分配的具有给定长度的缓冲区将cJSON实体渲染为文本。
    成功时返回1,失败时返回0。/
    /
    注意:cJSON在估计它将使用多少内存时并不总是100%准确,
    所以为了安全,要比实际需要多分配5个字节 */
    CJSON_PUBLIC(cJSON_bool) cJSON_PrintPreallocated(cJSON *item,
    char *buffer,
    const int length,
    const cJSON_bool format);

  6. /* 删除一个cJSON实体和所有子实体。 */
    CJSON_PUBLIC(void) cJSON_Delete(cJSON *item);

  7. /* 返回数组(或对象)中的项数。*/
    CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);

  8. /* 从数组"array"中检索项目号"index"。如果不成功,返回NULL。*/
    CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);

  9. /* 检查项目类型并返回其值 */
    CJSON_PUBLIC(char *) cJSON_GetStringValue(const cJSON * const item);
    CJSON_PUBLIC(double) cJSON_GetNumberValue(const cJSON * const item);

  10. /* 这些函数检查项的类型 */
    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);

  11. /* 这些调用创建适当类型的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);

  12. /* 在valuestring引用字符串的地方创建一个字符串,这样它就不会被cJSON_Delete释放 */
    CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string);

  13. /* 创建一个对象/数组,只引用它的元素,这样它们就不会被cJSON_Delete释放 */
    CJSON_PUBLIC(cJSON *) cJSON_CreateObjectReference(const cJSON *child);
    CJSON_PUBLIC(cJSON *) cJSON_CreateArrayReference(const cJSON *child);

  14. /* 这些实用程序创建计数项的数组。参数计数不能大于数字数组中的元素数量,否则数组访问将超出限制。 */
    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);

  15. /* 将项附加到指定的数组/对象。 */
    CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToArray(cJSON *array, cJSON *item);
    CJSON_PUBLIC(cJSON_bool) cJSON_AddItemToObject(cJSON *object,
    const char *string,
    cJSON *item);

  16. /* 从数组/对象中删除/分离项。 */
    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);

  17. /* 更新数组项。 */
    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);

  18. /* 复制一个cJSON项 */
    CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse);

  19. /* 缩小字符串,从字符串中删除空白字符(如’ ‘,’\t’, ‘\r’, ‘\n’)。输入指针json不能指向只读地址区,比如字符串常量,但应该指向可读可写的地址区 */
    CJSON_PUBLIC(void) cJSON_Minify(char *json);

  20. /* 同时向对象中创建和添加项的帮助函数。成功时返回添加的项,失败时返回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);

  21. 编译命令
    gcc *.c cJSON.c -lm

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值