cJSON代码阅读(3)——cJSON的数据结构

cJSON中主要的数据结构是cJSON结构体:

/* The cJSON structure: */
// cJSON对象
typedef struct cJSON {
    // 下一个、上一个json对象
	struct cJSON *next,*prev;	/* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */

    // 字节点
    struct cJSON *child;		/* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */

    // 对象类型:整形、字符串、浮点、布尔、对象、数组等
	int type;					/* The type of the item, as above. */

    // 不同的类型对应的值
	char *valuestring;			/* The item's string, if type==cJSON_String */
	int valueint;				/* The item's number, if type==cJSON_Number */
	double valuedouble;			/* The item's number, if type==cJSON_Number */
    // 节点的名字
	char *string;				/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
} cJSON;

json节点的类型有:

/* cJSON Types: */
// json节点的类型
// 布尔true/false
#define cJSON_False 0
#define cJSON_True 1
// null
#define cJSON_NULL 2
// 数值整形
#define cJSON_Number 3
// 字符串
#define cJSON_String 4
// 数组
#define cJSON_Array 5
// 对象
#define cJSON_Object 6
// 节点是否被引用
#define cJSON_IsReference 256
// json字符串是否为const
#define cJSON_StringIsConst 512

内存分配与回收的钩子(默认是系统的malloc和free):

// 节点的分配/回收函数(钩子)
typedef struct cJSON_Hooks {
      void *(*malloc_fn)(size_t sz);
      void (*free_fn)(void *ptr);
} cJSON_Hooks;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
cJSON是一个用于解析和生成JSON数据的C语言库。cJSON最为核心的就是cJSON结构体,该结构体包含了一些指针和数据成员,用于存储JSON的各个部分。其中,next和prev指针分别指向下一个和上一个兄弟节点,child指针指向子节点,type表示值的类型,valuestring和valueint分别存储字符串和整型数据,valuedouble存储浮点型数据,string存储名称。 cJSON提供了一个函数 cJSON_Parse,用于将JSON字符串解析为cJSON数据结构。该函数接受一个参数 value,即待解析的JSON字符串,并返回一个指向cJSON结构体的指针。 在使用cJSON时,需要在编译时链接cJSON库,并且需要执行一些指令。比如,使用gcc编译creat_json.c文件,并链接cJSON库,可以执行以下指令: gcc creat_json.c cJSON.c -I ./ -o creat 其中,-I选项指定头文件路径,-o选项指定生成的可执行文件名。 综上所述,cJSON是一个用于解析和生成JSON数据的C语言库,其核心是cJSON结构体,包含了各种指针和数据成员用于存储JSON数据的不同部分。通过调用cJSON_Parse函数,可以将JSON字符串解析为cJSON数据结构。在使用cJSON时,需要链接cJSON库,并执行相应的指令进行编译和生成可执行文件。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [cJSON详解](https://blog.csdn.net/weixin_45861321/article/details/115209555)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [cjson学习篇三:cJSON数据结构和设计思想](https://blog.csdn.net/weixin_42645653/article/details/120264644)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值