一、JSON概述
1.1 JSON介绍
JSON:JavaScript对象表示法(JavaScript Object Notation)。是一种轻量级的数据交换格式。
它基于ECMAScript的一个子集。
JSON采用完全独立于语言的文本格式,但是也使用了类似C语音家族的习惯(包括C、C++、C#、Java、JavaScript、Perl、Python等)。
这些特性使JSON成为理想的数据交换语言。
易于人阅读和编写,同时也易于机器解析和生成(一般用于提升网络传输速率)。
1.2 JSON语法
JSON语法是JavaScript对象表示法语法的子集:
- 数据在键/值对中;数据由逗号分隔;花括号保存对象,也称一个文档对象;方括号保存数组,每个数组成员用逗号隔开,并且每个数组成员可以是文档对象或者数组或者键值对。
JSON的三种语法:
- 键/值对key:value,用半角冒号分割。比如"name":"Faye"。文档对象JSON对象写在花括号中,可以包含多个键/值对。比如{"name":"Faye", "address":"北京"}。数组JSON数组在方括号中书写:数组成员可以是对象、值,也可以是数组(只要有意义)。比如{"love": ["乒乓球","高尔夫","斯诺克","羽毛球","LOL","撩妹"]}。
JSON的值可以是:
- 数字(整数或浮点数)、字符串(在双引号中)、逻辑值(true或false)、数组(在方括号中)、对象(在花括号中)、null。
JSON结构有两种:
- 对象: 对象在js中表示为“{}”括起来的内容,数据结构为**{key:value,key:value,...}**的键值对的结构,在面向对象的语言中,key为对象的属性,value为对应的属性值,所以很容易理解,取值方法为 对象.key 获取属性值;数组: 数组在js中是中括号“[]”括起来的内容,数据结构为["java","javascript","vb",...],取值方式和所有语言中一样,使用索引获取。 经过对象、数组2种结构就可以组合成复杂的数据结构了。
举例:
{
/*整型键值对*/
"student_num": 3,
/*数组*/
"stuinfo":[//数组开始
/*数组元素*/
{
"name": "彭于晏",
"no": "9527",
"sex": "男"
},
{
"name": "赵露思",
"no": "9528",
"sex": "女"
},
{
"name": "沈腾",
"sex": "male",
"no": "9529"
}
],//数组结尾
"rc": 0,
/*字符串键值对*/
"operation": "CALL",
"service": "telephone"
}
二、cJSON库介绍
cJSON是一个仅有一个.h文件,一个.c文件组成的JSON解析器,它是由纯C(ANSI C89)实现的,跨平台性较好。cJSON是采用链表存储的。
源码获取地址
在GitHub上的地址为:
https://github.com/DaveGamble/cJSON
移植
cJSON库在使用的时候只需要如下两步:
- 将cJSON.c(或者库文件)和cJSON.h添加到项目中即可;如果在命令行中进行链接还需要加上-lm表示链接math库。
彭老师的测试代码如下:
peng@ubuntu:~/work/test/cjson$ ls
cJSON.c cJSON.h main.c
main.c是彭老师编写的测试程序, 编译命令如下:
gcc cJSON.c main.c -o run -lm
三、常用函数api
1. cJSON_Parse
/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */
CJSON_PUBLIC(cJSON *) cJSON_Parse(const char *value);
作用:将一个JSON字符串,按照cJSON结构体的结构序列化整个数据包,并在堆中开辟一块内存存储cJSON结构体。
返回值:成功返回一个指向内存块中的cJSON的指针,失败返回NULL。
2. cJSON_Delete
/* Delete a cJSON entity and all subentities. */
CJSON_PUBLIC(void) cJSON_Delete(cJSON *c);
作用:释放位于堆中cJSON结构体内存。
返回值:无
注意:在使用cJSON_Parse()获取cJSON指针后,若不再使用了,则需要调用cJSON_Delete()对其释放,否则会导致内存泄漏。
3. cJSON_Print
/* Render a cJSON entity to text for transfer/storage. */
CJSON_PUBLIC(char *) cJSON_Print(const cJSON *item);
/* Render a cJSON entity to text for transfer/storage without any formatting. */
CJSON_PUBLIC(char *) cJSON_PrintUnformatted(const cJSON *item);
作用:将cJSON数据解析成JSON字符串,并在堆中开辟一块char*的内存空间存储JSON字符串。cJSON_PrintUnformatted()与cJSON_Print()类似,只是打印输出不带格式,而只是一个字符串数据。
返回值:成功返回一个char*指针该指针指向位于堆中JSON字符串,失败返回NULL。
注意:通过cJSON_Print()可以将cJSON链表中所有的cJSON对象打印出来,但是需要手动去释放对应的资源:free(char *)。
4. cJSON_Version
/* returns the version of cJSON as a string */
CJSON_PUBLIC(const char*) cJSON_Version(void);
作用:获取当前使用的cJSON库的版本号。
返回值:返回一个字符串数据。
5. cJSON_GetArraySize/cJSON_GetArrayItem
/* Returns the number of items in an array (or object). */
CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array);
作用:获取数组cJSON对象数组元素个数。返回值:成功返回数组元素个数。
/* Retrieve item number "index" from array "array". Returns NULL if unsuccessful. */
CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index);
作用:从数组cJSON对象获取下标为index的对向。返回值:成功返回一个指向内存块中的cJSON的指针,失败返回NULL。
/* Get item "string" from object. Case insensitive. */
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON * const object, const char * const string);
CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string);
CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *string);
/* Check if the item is a string and return its valuestring */
CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item);
6. 类型判断接口
下面的接口用于判断具体cJSON指针指向的item类型:
/* These functions check the type of an item */
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);
7. 创建cJSON对象接口
下面的接口用于创建指定类型的cJSON数据结构对象:
/* raw json */
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(constchar *raw);
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateObject(void);
/* These calls create a cJSON item of the appropriate type. */
CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
CJSON_PUBLIC(cJSON *) cJSON_CreateString(constchar *string);
/* These utilities create an Array of count items. */
CJSON_PUBLIC(cJSON *) cJSON_CreateIntArray(constint *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateFloatArray(constfloat *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateDoubleArray(constdouble *numbers, int count);
CJSON_PUBLIC(cJSON *) cJSON_CreateStringArray(constchar **strings, int count);
8. 添加cJSON对象到链表
下面的接口用于将创建的cJSON对象添加到链表中:
/* Append item to the specified array/object. */
CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item);
CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
9. 从cJSON对象中删除一个cJSON对象
下面的接口用于从现有的cJSON链表中删除一个对象:
/* Remove/Detatch items from Arrays/Objects. */
CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string);
CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string);
10. 创建并添加到链表
下面的接口用于创建并添加cJSON对象到指定的链表(简化操作):
/* Helper functions for creating and adding items to an object at the same time.
* They return the added item or NULL on failure. */
CJSON_PUBLIC(cJSON*) cJSON_AddNullToObject(cJSON * const object, constchar * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, constchar * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, constchar * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, constchar * const name, const cJSON_bool boolean);
CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, constchar * const name, constdouble number);
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, constchar * const name, constchar * conststring);
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, constchar * const name, constchar * const raw);
CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, constchar * const name);
CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, constchar * const name);
四、参考代码
1、 打开json文件代码
char * get_cjsonstr_from_file(char * filename)
{
FILE *fp = fopen(filename,"r");
int file_size = 0;
char * jsonStr = NULL;
if(fp== NULL){
cprintf(RED,"错误:无法打开文件%sn",filename);
returnNULL;
}
//获取文件大小
fseek(fp, 0,SEEK_END);
file_size = ftell(fp);
cprintf(YEL_H,"filelsize=%dn",file_size);
fseek(fp, 0, SEEK_SET);// 读取整个文件内容
jsonStr = (char*)malloc(file_size + 1);
if(jsonStr==NULL){
cprintf(RED,"错误:内存分配失败n");
fclose(fp);
returnNULL;
}
fread(jsonStr, 1, file_size, fp);
jsonStr[file_size] = '';
fclose(fp);
return jsonStr;
}
注意:该函数申请了内存,所以调用该函数,解析完毕,要把申请的内存free,
jsonStr = get_cjsonstr_from_file(CJSON_FILE_NAME);
if(jsonStr == NULL)
{
return -1;
}
myParse(jsonStr);
free(jsonStr);
2、解析json文件代码如下:
下面是解析json字符串的所有键/值的内容:
打印结果如下:
3、 修改json文件某个值
我们尝试修改json字符串
-
-
- 根目录下增加节点classno
-
-
-
- 根目录下增加节点test,并将新建的classno节点添加到test节点下
-
-
-
- 删除stuinfo数组index为2的元素
-
-
- 新建数组元素到stuinfo数组下
代码与修改前后json字符串信息如下:
4、其他
还可以将整个cjson对向内容打出来 有格式的方式打印Json:
cprintf(GREEN_H,"%snn", cJSON_Print(root));
无格式方式打印json:
cprintf(GREEN_H,"%snn", cJSON_PrintUnformatted(root));
完整代码获取,公众号【一口Linux 】后台回复:cjson
215