CJSON源码分析

CJSON的数据结构

/* The cJSON structure: */
typedef struct cJSON
{
    /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem  next/prev结构体指针允许你遍历数组/对象链。或者使用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. 一个数组/对象会有一个child指针,指向数组/对象中的头结点*/
    struct cJSON *child;

    /* The type of the item, as above. 本JSON项的类型*/
    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. 本CJSON项的名称,若本项是一个OBJECT的下级,或其内容,会拥有名称;如{"name":"hany"}, "hany"是一个string型的JSON值,而它的名称是"name" */
    char *string;
} cJSON;

image

常见JSON串

空串

{}
image

简单情况

{"name":"hany", "age":18}
image

一般情况

{
	"users":[
		{"uid":123},
		{"uid":456}
	]
}

image

几个函数

parse_object

image

parse_array

image

posted @ 2021-09-28 18:10  海林的菜园子  阅读(99)  评论(0编辑  收藏  举报