cJSON库的使用
一、问题引入
有一批数据以json格式进行存储,如今要分析数据,那么第一步要读取数据中的json。
json作为比较流行和通用的数据存储和传输的格式,一般都会有通用的开源库,切记盲目的重复造轮子。
经过一番查询,找了一个cJSON库,cJSON库是使用C语言编写的开源库,主要功能是处理json。
二、解决过程
2-1 cJSON库操作--增加键值对
// 在cJSON指针对象中增加一个键值对,其中值的属性为`string`
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string)
// 在cJSON指针对象中增加一个键值对,其中值的属性为`number`,既可以是整形,也可以是浮点型
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const double number)
Example_1: 在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;
}
运行结果:

2-2 cJSON库操作--删除键值对
// 在cJSON指针对象中删除指定键值对
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string)
Example_2: 在json中删除键值对age
#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;
}
运行结果:

2-3 cJSON库操作--修改键值对
// 在cJSON指针对象中修改指定键值对的值,值的属性为number,既可以是整型,也可以是浮点型
CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number)
// 在cJSON指针对象中修改指定键值对的值,值的属性为string
CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring)
Example_3: 在json中修改键值对的值
#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;
}
运行结果:

2-4 cJSON库操作--查找键值对
// 在cJSON指针对象中通过键查找值
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string)
Example_4: 在json中查找键值对age
#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;
}
运行结果:

三、反思总结
json在数据传输和存储使用的极多,cJSON库是用c语言编写用于处理json的开源库。那么对于json的基础操作有必要记录下,json的基本操作:键值对的查询、键值对的删除、键值对的修改、键值对的增加等。
后续应对cJSON库源码分析解读,从源码级别理解,提高编程水平。

浙公网安备 33010602011771号