首页 > 其他分享 >cJSON库的使用

cJSON库的使用

时间:2022-12-03 17:34:08浏览次数:65  
标签:cJSON char cjson json pars 使用 键值

cJSON库的使用

cJSON库是使用C语言编写的开源库,主要功能是处理json。

一、cJSON库操作--增加键值对

❤ API 函数:CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string)

功能:在cJSON指针对象中增加一个键值对,其中值的属性为string

❤ API 函数:CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number)

功能:在cJSON指针对象中增加一个键值对,其中值的属性为number,既可以是整形,也可以是浮点型


在json中增加三个键值对:"sex":"male""math":90.5"class":1107

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


int main(void)
{
	char json[999] = { "{\"name\":\"ZhangSan\",\"age\":20,\"height\":180,\"weight\":60}" };
	cJSON *cjson_pars = cJSON_Parse(json);
	if (NULL == cjson_pars)
	{
		return -1;
	}

	char *p_json = NULL;
	p_json = cJSON_PrintUnformatted(cjson_pars);

	printf("增加键值对之前\n\n");
	if (p_json)
	{
		printf("%s\n\n", p_json);
	}

	cJSON_AddStringToObject(cjson_pars, "sex", "male");
	cJSON_AddNumberToObject(cjson_pars, "math", 90.50);
	cJSON_AddNumberToObject(cjson_pars, "class", 1107);

	p_json = cJSON_PrintUnformatted(cjson_pars);

	printf("增加键值对之后\n\n");
	if (p_json)
	{
		printf("%s\n\n", p_json);
	}

	/* cJSON_PrintUnformatted() and cJSON_Print() 函数的返回值char * 需要用户释放,否则会造成内存泄露. */
	cJSON_free(p_json);
	/* cJSON_Parse() 函数的返回值cJSON * 需要用户自己释放,否则会造成内泄露. */
	cJSON_Delete(cjson_pars);

	return 0;
}

运行结果:

二、cJSON库操作--删除键值对

❤ API 函数:CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);

功能:在cJSON指针对象中删除指定键值对

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

int main (void)
{
	char json[999] = { "{\"name\":\"ZhangSan\",\"age\":20,\"height\":180,\"weight\":60}" };
	cJSON *cjson_pars = cJSON_Parse(json);
	if (NULL == cjson_pars)
	{
		return -1;
	}

	char *p_json = NULL;
	p_json = cJSON_PrintUnformatted(cjson_pars);

	printf("删除键值对之前\n\n");
	if (p_json)
	{
		printf("%s\n\n", p_json);
	}

	cJSON_DeleteItemFromObject(cjson_pars, "age");

	p_json = cJSON_PrintUnformatted(cjson_pars);

	printf("删除键值对之后\n\n");
	if (p_json)
	{
		printf("%s\n\n", p_json);
	}

	/* cJSON_PrintUnformatted() and cJSON_Print() 函数的返回值char * 需要用户释放,否则会造成内存泄露. */
	cJSON_free(p_json);
	/* cJSON_Parse() 函数的返回值cJSON * 需要用户自己释放,否则会造成内泄露. */
	cJSON_Delete(cjson_pars);
	return 0;
}

运行结果:

三、cJSON库操作--修改键值对

❤ API 函数:CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number)

功能:在cJSON指针对象中修改指定键值对的值,值的属性为number,既可以是整型,也可以是浮点型

❤ API 函数:CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring)

功能:在cJSON指针对象中修改指定键值对的值,值的属性为string

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

int main(void)
{
	char json[999] = { "{\"name\":\"ZhangSan\",\"age\":20,\"height\":180,\"weight\":60}" };
	cJSON *cjson_pars = cJSON_Parse(json);
	if (NULL == cjson_pars)
	{
		return -1;
	}

	char *p_json = NULL;
	p_json = cJSON_PrintUnformatted(cjson_pars);

	printf("修改键值对的值之前\n\n");
	if (p_json)
	{
		printf("%s\n\n", p_json);
	}

	cJSON *key_age = cJSON_GetObjectItem(cjson_pars, "age");
	if (NULL != key_age)
	{
		cJSON_SetNumberHelper(key_age, 18);
	}

	p_json = cJSON_PrintUnformatted(cjson_pars);

	printf("修改键值对的值之后\n\n");
	if (p_json)
	{
		printf("%s\n\n", p_json);
	}

	/* cJSON_PrintUnformatted() and cJSON_Print() 函数的返回值char * 需要用户释放,否则会造成内存泄露. */
	cJSON_free(p_json);
	/* cJSON_Parse() 函数的返回值cJSON * 需要用户自己释放,否则会造成内泄露. */
	cJSON_Delete(cjson_pars);

	return 0; 
}

运行结果:

四、cJSON库操作--查找键值对

❤ API 函数:CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string)

功能:在cJSON指针对象中通过键查找值

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

int main(void)
{
	char json[999] = { "{\"name\":\"ZhangSan\",\"age\":20,\"height\":180,\"weight\":60}" };
	cJSON *cjson_pars = cJSON_Parse(json);
	if (NULL == cjson_pars)
	{
		return -1;
	}

	char *p_json = NULL;
	p_json = cJSON_PrintUnformatted(cjson_pars);

	printf("cJONS 键值对\n\n");
	if (p_json)
	{
		printf("%s\n\n", p_json);
	}

	cJSON *key_age = cJSON_GetObjectItem(cjson_pars, "age");
	printf("查找键\"age\"的值\n");
	if (NULL != key_age)
	{
		printf("successful -- \"age\":%d\n", key_age->valueint);
	}
	else
	{
		printf("failure\n");
	}

	/* cJSON_PrintUnformatted() and cJSON_Print() 函数的返回值char * 需要用户释放,否则会造成内存泄露. */
	cJSON_free(p_json);
	/* cJSON_Parse() 函数的返回值cJSON * 需要用户自己释放,否则会造成内泄露. */
	cJSON_Delete(cjson_pars);

	return 0;
}

运行结果:

参考来源

C/C++ 使用cjson库 操作Json格式文件(创建、插入、解析、修改、删除)

标签:cJSON,char,cjson,json,pars,使用,键值
From: https://www.cnblogs.com/caojun97/p/16943603.html

相关文章