cJSON创建和解析使用接口分析

cJSON是一个c语言编写的构建和解析json格式数据的库

源码在这里下载    https://github.com/DaveGamble/cJSON

主要就两个文件cJSON.c和cJSON.h,直接一起链接进来就可以了

 

创建json

这里面最重要的一个结构为cJSON

/* 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对象成员

prev,节点前趋指针,指向前一个cJSON节点

next,节点后继指针,指向后一个cJSON节点

child,当一个键值对key,value中,value为Array或Object时,child才有指向,否则child=NULL

下面的例子里,child是这样的指向

value为object(type=cJSON_Object)时,child指向object中第一个元素

score->type=cJSON_Object;

score->child =Chinese;  score->next=friend;

Chinese->next=English; Chinese->child=NULL;

English->next=Math;

当value为Array(type=CJSON_Array)时,child指向此Array中的第一个元素

friend->type=cJSON_Array;

friend->child=friend1;

friend1->next=friend2;

type,代表key,value键值对中value的类型

/* 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 */

valuestring,当类型为cJSON_String 或cJSON_Raw时,value的值,type不符时为NULL

valueint,当类型为cJSON_Number时,value的值

valuedouble,当类型为cJSON_NUmber时,value的值(主要用这个)

string, 代表key,value键值对中key的值

cJSON实际上是一个双向链表,还是举个例子好了

{
	"name":	"luoc",
	"age":	16,
	"sex":	"male",
	"student":	true,
	"score":	{
		"Chinese":	92,
		"English":	76,
		"Math":	83,
		"Physical":	88
	},
	"friend":	[{
			"name":	"chenx",
			"sex":	"female",
			"age":	15
		}, {
			"name":	"yanl",
			"sex":	"male",
			"age":	16
		}, {
			"name":	"xiax",
			"sex":	"male",
			"age":	16
		}]
}

如果要创建这么一个json格式的文件,需要6个cJSON结构(有几对括号,就需要几个结构)

代码如下

#include <stdio.h>
#include <stdbool.h>
#include "cJSON.h"

int main(int argc, char *argv[])
{
        cJSON *item = NULL;
        cJSON *score = NULL;
        cJSON *friend = NULL;
        cJSON *friend1 = NULL;
        cJSON *friend2 = NULL;
        cJSON *friend3 = NULL;

        //    最外层的大括号
        item = cJSON_CreateObject();

        cJSON_AddStringToObject(item, "name", "luoc");
        cJSON_AddStringToObject(item, "sex", "male");
        cJSON_AddNumberToObject(item, "age", 16);
        cJSON_AddBoolToObject(item, "student", true);

        //     score   
        score = cJSON_CreateObject();
        cJSON_AddNumberToObject(score, "Chinese", 92);
        cJSON_AddNumberToObject(score, "English", 76);
        cJSON_AddNumberToObject(score, "Math", 83);
        cJSON_AddNumberToObject(score, "Physical", 88);

        cJSON_AddItemToObject(item, "score", score);

        //    friend
        friend = cJSON_CreateArray();
        cJSON_AddItemToObject(item, "friend", friend);

        //    friend1
        friend1 = cJSON_CreateObject();
        cJSON_AddStringToObject(friend1, "name", "chenx");
        cJSON_AddStringToObject(friend1, "sex", "female");
        cJSON_AddNumberToObject(friend1, "age", 15);

        //    friend2
        friend2 = cJSON_CreateObject();
        cJSON_AddStringToObject(friend2, "name", "yanl");
        cJSON_AddStringToObject(friend2, "sex", "male");
        cJSON_AddNumberToObject(friend2, "age", 16);

        //    friend3
        friend3 = cJSON_CreateObject();
        cJSON_AddStringToObject(friend3, "name", "xiax");
        cJSON_AddStringToObject(friend3, "sex", "male");
        cJSON_AddNumberToObject(friend3, "age", 16);

        cJSON_AddItemToArray(friend, friend1);
        cJSON_AddItemToArray(friend, friend2);
        cJSON_AddItemToArray(friend, friend3);

//      printf("%s\n\n", cJSON_Print(item));

        FILE *fp = fopen("my.json", "a+");
        if(fp)
                fprintf(fp, "%s", cJSON_Print(item));
        fclose(fp);

        cJSON_Delete(item);

        return 0;
}

解析json

还是解析上面的json串吧,直接上代码

#include <stdio.h>
#include "cJSON.h"

int main(int argc, char *argv[])
{
	char *str = "{			\
		\"name\":	\"luoc\",		\
		\"age\":	16,			\
		\"sex\":	\"male\",		\
		\"student\":	true,			\
		\"score\":	{			\
			\"Chinese\":	92,		\
			\"English\":	76,		\
			\"Math\":	83,		\
			\"Physical\":	88		\
		},					\
		\"friend\":	[{				\
				\"name\":	\"chenx\",	\
				\"sex\":	\"female\",	\
				\"age\":	15		\
			}, {					\
				\"name\":	\"yanl\",	\
				\"sex\":	\"male\",	\
				\"age\":	16		\
			}, {					\
				\"name\":	\"xiax\",	\
				\"sex\":	\"male\",	\
				\"age\":	16		\
			}]					\
		}";
		
	cJSON *item = cJSON_Parse(str);
	if(item == NULL)
	{
		printf("Parse error\n");
		goto end;
	}

	cJSON *name = cJSON_GetObjectItem(item, "name");
	if(name == NULL)
	{
		printf("No item 'name'\n");
		goto end;
	}
	if(cJSON_IsString(name))
		printf("name=[%s]\n", name->valuestring);

	cJSON *score = cJSON_GetObjectItem(item, "score");
	cJSON *score_print = cJSON_Print(score);
	printf("score=[%s]\n", score_print);
    //    cJSON_Print会malloc空间,需要free
	cJSON_free(score_print);

	cJSON *friend = cJSON_GetObjectItem(item, "friend");
	printf("friend is a array=[%s]\n", cJSON_IsArray(friend) ? "true" : "false");
	int size = cJSON_GetArraySize(friend);

	printf("friend count =[%d]\n", size);
	int i = 0;
	cJSON *curr = NULL;
	curr = friend->child;
	for(i = 0; i < size; i++)
	{
		cJSON *name = cJSON_GetObjectItem(curr, "name");
		printf("friend%d is named [%s]\n", i, name->valuestring);	

		curr = curr->next;
	}

end:
	if(item)
		cJSON_Delete(item);
	return 0;
}

主要是 cJSON_Parse,解析后返回的cJSON对象,是这个双向链表的head指针然后可以通过cJSON_GetObjectItem来一个个得获取最外层的节点,根据type来判断节点value类型

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);

不同节点,取自己需要的value就行了

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
cJSON是一个用于解析和生成JSON数据的C语言库。它提供了一组API函数,使得用户可以方便地解析和生成JSON数据。以下是cJSON库中常用的解析接口: 1. cJSON_Parse:将JSON字符串解析成cJSON节点 ``` cJSON *cJSON_Parse(const char *value); ``` cJSON_Parse函数接受一个JSON字符串作为输入参数,并返回一个cJSON节点。如果解析成功,则返回的节点表示整个JSON文档的根节点;否则返回NULL。调用者需要手动释放返回的节点。 2. cJSON_GetObjectItem:获取对象节点中指定名称的子节点 ``` cJSON *cJSON_GetObjectItem(const cJSON *object, const char *string); ``` cJSON_GetObjectItem函数接受两个参数:对象节点和字符串名称。如果对象节点是一个JSON对象(即类型为cJSON_Object),则返回该对象中名称为string的子节点;否则返回NULL。 3. cJSON_GetArrayItem:获取数组节点中指定索引的子节点 ``` cJSON *cJSON_GetArrayItem(const cJSON *array, int index); ``` cJSON_GetArrayItem函数接受两个参数:数组节点和索引值。如果数组节点是一个JSON数组(即类型为cJSON_Array),则返回该数组中索引为index的子节点;否则返回NULL。 4. cJSON_GetStringValue:获取字符串节点的值 ``` const char *cJSON_GetStringValue(const cJSON *object); ``` cJSON_GetStringValue函数接受一个参数:字符串节点。如果字符串节点是一个JSON字符串(即类型为cJSON_String),则返回该节点中的字符串值;否则返回NULL。 以上是常用的cJSON解析接口,当然还有很多其他的接口可以帮助用户完成JSON数据的解析和生成。在使用cJSON库时,需要仔细阅读cJSON.h头文件中的API文档,以便正确地使用各种接口。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值