cJSON简析

json概要

json是一种文本格式的协议
对于人的可阅读性非常好

在这里插入图片描述
其中object和array中的value都可以嵌套

cJSON数据结构

每个节点的数据结构如下

/* cJSON Types: */
#define cJSON_Invalid (0)
#define cJSON_False  (1 << 0)
#define cJSON_True   (1 << 1)
#define cJSON_NULL   (1 << 2)
#define cJSON_Number (1 << 3)
#define cJSON_String (1 << 4)
#define cJSON_Array  (1 << 5)
#define cJSON_Object (1 << 6)
#define cJSON_Raw    (1 << 7) /* raw json */

#define cJSON_IsReference 256
#define cJSON_StringIsConst 512

/* The cJSON structure: */
typedef struct cJSON
{
    /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */
    struct cJSON *next;
    struct cJSON *prev;
    /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */
    struct cJSON *child;

    /* The type of the item, as above. */
    int type;

    /* The item's string, if type==cJSON_String  and type == cJSON_Raw */
    char *valuestring;
    /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
    int valueint;
    /* The item's number, if type==cJSON_Number */
    double valuedouble;

    /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
    char *string;
} cJSON;

递归解析

入口函数:
cJSON_ParseWithLengthOpts(...)
{
	parse_value(...) // 解析值
}
parse_value(...)
{
	if(...) { ->type = cJSON_NULL; return true; }
	if(...) { ->type = cJSON_False; return true; }
	if(...) { ->type = cJSON_True; return true; }
	if(...) { return parse_string(...); }
	if(...) { return parse_number(...); }
	if(...) { return parse_array(...); }
	if(...) { return parse_object(...); }
	
}
parse_string(...)
{
	...
}
parse_number(...)
{
	...
}
parse_array(...)
{
	do {
		...
		parse_value(...);
		...
    } while(... && x==',');
	...
}
parse_object(...)
{
	do {
		...
		cJSON *new_item = cJSON_New_Item();
		current_item->next = new_item;
        new_item->prev = current_item;
        current_item = new_item;
        parse_string(current_item, input_buffer);
        current_item->string = current_item->valuestring;
        current_item->valuestring = NULL;
		parse_value(...);
		...
    } while(... && x==',');
	...
}

示例

{
    "name": "Awesome 4K",
    "resolutions": [
        {
            "width": 1280,
            "height": 720
        },
        {
            "width": 1920,
            "height": 1080
        },
        {
            "width": 3840,
            "height": 2160
        }
    ]
}

对应的树状结构:
在这里插入图片描述

node1:

struct cJSON *next = 0
struct cJSON *prev = 0
struct cJSON *child = node2
int type = cJSON_Object
char *valuestring = 0
int valueint = 0
double valuedouble = 0
char *string = 0

node2:

struct cJSON *next = node3
struct cJSON *prev = 0
struct cJSON *child = 0
int type = cJSON_String;
char *valuestring = alloc("Awesome 4K")
int valueint = 0
double valuedouble = 0
char *string = alloc("name")

node3:

struct cJSON *next = 0
struct cJSON *prev = node2
struct cJSON *child = node4
int type = cJSON_Array
char *valuestring = 0
int valueint = 0
double valuedouble = 0
char *string = alloc("resolutions")

node4:

struct cJSON *next = node5
struct cJSON *prev = 0
struct cJSON *child = node7
int type = cJSON_Object
char *valuestring = 0
int valueint = 0
double valuedouble = 0
char *string = 0

node5:

struct cJSON *next = node6
struct cJSON *prev = node4
struct cJSON *child = node9
int type = cJSON_Object
char *valuestring = 0
int valueint = 0
double valuedouble = 0
char *string = 0

node6:

struct cJSON *next = 0
struct cJSON *prev = node5
struct cJSON *child = node11
int type = cJSON_Object
char *valuestring = 0
int valueint = 0
double valuedouble = 0
char *string = 0

node7:

struct cJSON *next = node8
struct cJSON *prev = 0
struct cJSON *child = 0
int type = cJSON_Number
char *valuestring = 0
int valueint = 1280
double valuedouble = 1280
char *string = alloc("width")

node8:

struct cJSON *next = 0
struct cJSON *prev = node7
struct cJSON *child = 0
int type = cJSON_Number
char *valuestring = 0
int valueint = 720
double valuedouble = 720
char *string = alloc("height")

node9:

struct cJSON *next = node10
struct cJSON *prev = 0
struct cJSON *child = 0
int type = cJSON_Number
char *valuestring = 0
int valueint = 1920
double valuedouble = 1920
char *string = alloc("width")

node10:

struct cJSON *next = 0
struct cJSON *prev = node9
struct cJSON *child = 0
int type = cJSON_Number
char *valuestring = 0
int valueint = 1080
double valuedouble = 1080
char *string = alloc("height")

node11:

struct cJSON *next = node12
struct cJSON *prev = 0
struct cJSON *child = 0
int type = cJSON_Number
char *valuestring = 0
int valueint = 3840
double valuedouble = 3840
char *string = alloc("width")

node12:

struct cJSON *next = 0
struct cJSON *prev = node11
struct cJSON *child = 0
int type = cJSON_Number
char *valuestring = 0
int valueint = 2160
double valuedouble = 2160
char *string = alloc("height")

references

https://github.com/DaveGamble/cJSON/tree/master

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值