击左上方蓝色“一口Linux”,选择“设为星标”
第一时间看干货文章 ☞【干货】嵌入式驱动工程师学习路线 ☞【干货】Linux嵌入式知识点-思维导图-免费获取 ☞【就业】一个可以写到简历的基于Linux物联网综合项目 ☞【就业】简历模版

JSON:JavaScript对象表示法(JavaScript Object Notation)。是一种轻量级的数据交换格式。
它基于ECMAScript的一个子集。
JSON采用完全独立于语言的文本格式,但是也使用了类似C语音家族的习惯(包括C、C++、C#、Java、JavaScript、Perl、Python等)。
这些特性使JSON成为理想的数据交换语言。
易于人阅读和编写,同时也易于机器解析和生成(一般用于提升网络传输速率)。
JSON语法是JavaScript对象表示法语法的子集:
JSON的三种语法:
JSON的值可以是:
JSON结构有两种:
举例:
{/*整型键值对*/"student_num": 3,/*数组*/"stuinfo":[//数组开始/*数组元素*/ {"name": "彭于晏","no": "9527","sex": "男" }, {"name": "赵露思","no": "9528","sex": "女" }, {"name": "沈腾","sex": "male","no": "9529" } ],//数组结尾"rc": 0,/*字符串键值对*/"operation": "CALL","service": "telephone"}cJSON是一个仅有一个.h文件,一个.c文件组成的JSON解析器,它是由纯C(ANSI C89)实现的,跨平台性较好。cJSON是采用链表存储的。
在GitHub上的地址为:
https://github.com/DaveGamble/cJSONcJSON库在使用的时候只需要如下两步:
彭老师的测试代码如下:
peng@ubuntu:~/work/test/cjson$ lscJSON.c cJSON.h main.cmain.c是彭老师编写的测试程序, 编译命令如下:
gcc cJSON.c main.c -o run -lm/* Supply a block of JSON, and this returns a cJSON object you can interrogate. */CJSON_PUBLIC(cJSON *) cJSON_Parse(constchar *value);作用:将一个JSON字符串,按照cJSON结构体的结构序列化整个数据包,并在堆中开辟一块内存存储cJSON结构体。
返回值:成功返回一个指向内存块中的cJSON的指针,失败返回NULL。
/* Delete a cJSON entity and all subentities. */CJSON_PUBLIC(void) cJSON_Delete(cJSON *c);作用:释放位于堆中cJSON结构体内存。
返回值:无
注意:在使用cJSON_Parse()获取cJSON指针后,若不再使用了,则需要调用cJSON_Delete()对其释放,否则会导致内存泄漏。
/* 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 *)。
/* returns the version of cJSON as a string */CJSON_PUBLIC(constchar*) cJSON_Version(void);作用:获取当前使用的cJSON库的版本号。
返回值:返回一个字符串数据。
/* 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, constchar * conststring);CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, constchar * conststring);CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, constchar *string);/* Check if the item is a string and return its valuestring */CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item);下面的接口用于判断具体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);下面的接口用于创建指定类型的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);下面的接口用于将创建的cJSON对象添加到链表中:
/* Append item to the specified array/object. */CJSON_PUBLIC(void) cJSON_AddItemToArray(cJSON *array, cJSON *item);CJSON_PUBLIC(void) cJSON_AddItemToObject(cJSON *object, constchar *string, cJSON *item);下面的接口用于从现有的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, constchar *string);CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, constchar *string);CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, constchar *string);CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, constchar *string);下面的接口用于创建并添加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);char * get_cjsonstr_from_file(char * filename){ FILE *fp = fopen(filename,"r");int file_size = 0;char * jsonStr = NULL;if(fp== NULL){ cprintf(RED,"错误:无法打开文件%s\n",filename);returnNULL; }//获取文件大小 fseek(fp, 0,SEEK_END); file_size = ftell(fp); cprintf(YEL_H,"filelsize=%d\n",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] = '\0'; fclose(fp);return jsonStr;}注意:该函数申请了内存,所以调用该函数,解析完毕,要把申请的内存free,
jsonStr = get_cjsonstr_from_file(CJSON_FILE_NAME);if(jsonStr == NULL) {return-1; } myParse(jsonStr);free(jsonStr);下面是解析json字符串的所有键/值的内容:
打印结果如下:
我们尝试修改json字符串
代码与修改前后json字符串信息如下:
还可以将整个cjson对向内容打出来 有格式的方式打印Json:
cprintf(GREEN_H,"%s\n\n", cJSON_Print(root));无格式方式打印json:
cprintf(GREEN_H,"%s\n\n", cJSON_PrintUnformatted(root));

后台回复:cjson
end
一口Linux
关注,回复【1024】海量Linux资料赠送
精彩文章合集
文章推荐