cJSON函数用法
admin
2024-03-13 20:49:43
0次
  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

相关内容

热门资讯

AI做攻略、“双向奔赴”……国... 本文转自:人民网人民网记者 许维娜今年国庆假期,AI(人工智能)成了不少年轻人的“旅行搭子”。行前做...
合肥的这个“梗”, 翻篇了 社交平台的评论区,曾长期流传一句专属于合肥的黑色幽默。有人问合肥哪里好玩,本地网友半开玩笑地回复:合...
7万+旅游中高层都在的群,今天... 做旅游这行,资源卡位、渠道互通、项目落地——往往就差一个“对的机会”。我们的行业社群已聚集70,00...
一场帐篷音乐节,如何让井研从“... 帐篷音乐节现场封面新闻记者 周洪攀10月的研溪湿地,晚风里裹着柑橘的清甜与民谣的回响。夜幕降临,10...
白鹤翩翩落星愿,上海迪士尼度假... 10月9日,世界候鸟日前夕,上海迪士尼度假区星愿公园的湖风里多了一分白鹤的灵动。三座白鹤雕塑正式揭幕...
成都锦江边赏初秋枫叶飘飘 目前,成都锦江边国华街枫叶树红了。近年来,国华街附近的几条街都栽种了红枫树,是城区较集中看枫叶的点位...
GBTA:商务旅行正在复苏,但... 亚太正在成为全球商务旅行增长的重要引擎。在经历疫情后的快速反弹之后,全球商务旅行正在进入一个新的增长...
从景区焕新到节赛引流 邹城双节... 齐鲁网·闪电新闻10月9日讯 中秋、国庆“双节”假期,邹城市文化和旅游局提前谋划、周密部署,立足自身...
江山如此多娇 且看石楼今朝 金秋十月,黄河岸畔秋意盎然,丰收盛会喜迎国庆。本次石楼国庆文旅活动以“江山如此多娇,且看石楼今朝”为...
海南国庆假期接待游客475.4... 2026年国庆假期,海南接待游客475.40万人次,较2025年增长13.1%,较2024年增长15...