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

本文详细介绍了cJSON解析库的主要数据结构及节点类型,并解释了内存分配与回收的钩子机制。通过阅读本文,读者可以了解到cJSON结构体的具体组成及其在处理JSON数据时的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值