招商网站建设免费,青岛网站关键词,电子商务主要就业方向,做go富集的网站cJSON简介 CJSON库是一个用于解析和生成JSON数据的C语言库。 它提供了一组函数#xff0c;使得在C语言中操作JSON数据变得简单而高效。 您可以使用CJSON库来解析从服务器返回的JSON数据#xff0c;或者将C语言数据结构转换为JSON格式以进行传输。
cJSON 使用
官网地址使得在C语言中操作JSON数据变得简单而高效。 您可以使用CJSON库来解析从服务器返回的JSON数据或者将C语言数据结构转换为JSON格式以进行传输。
cJSON 使用
官网地址https://sourceforge.net/projects/cjson/
cJSON只有一个cjson.h 和cjson.c 文件可以很方便的集成到其他项目中。cJSON支持将JSON数据解析为cJSON对象也支持将cJSON对象转换为JSON数据。cJSON的使用非常简单只需要包含 cjson.h 头文件然后调用相应的API即可完成JSON数据的解析和生成。
cJSON 数据生成
新增cjson_demo1.c 文件
#includestdio.h
#includestring.h
#includestdlib.h
#include cJSON.h
/*** 目标1、引入cjson 库依赖文件(cJSON.c/cJSON.h)* 2、cjson 库基本使用 */
int main(){// 第一步创建cJSON 对象cJSON *jsonObject cJSON_CreateObject();// 第二步输出cJSON 对象char *content cJSON_Print(jsonObject);printf(%s\n, content);// 第三步cJSON 对象添加属性:基本属性之字符串cJSON_AddItemToObject(jsonObject, name, cJSON_CreateString(周志刚));content cJSON_Print(jsonObject);printf(%s\n, content);// 第三步cJSON 对象添加属性:基本属性之整形cJSON_AddItemToObject(jsonObject, age, cJSON_CreateNumber(32));content cJSON_Print(jsonObject);printf(%s\n, content);// 第三步cJSON 对象添加属性:基本属性之boolcJSON_AddItemToObject(jsonObject, man, cJSON_CreateBool(1));content cJSON_Print(jsonObject);printf(%s\n, content);// 第三步cJSON 对象添加属性:基本属性之NULLcJSON_AddItemToObject(jsonObject, woman, cJSON_CreateNull());content cJSON_Print(jsonObject);printf(%s\n, content);// 第四步cJSON 对象添加属性:复杂属性之数组cJSON * childs cJSON_CreateArray();cJSON_AddItemToArray(childs, cJSON_CreateString(周晨曦));cJSON_AddItemToArray(childs, cJSON_CreateString(周晨宇));cJSON_AddItemToObject(jsonObject, childs, childs);content cJSON_Print(jsonObject);printf(%s\n, content);// 第四步cJSON 对象添加属性:复杂属性之cJSON对象cJSON *wife cJSON_CreateObject();cJSON_AddItemToObject(wife, name, cJSON_CreateString(王珍));cJSON_AddItemToObject(jsonObject, wife, wife);content cJSON_Print(jsonObject);printf(%s\n, content);// 第五步输出json 字符串到指定文件FILE *file fopen(output.json, w);if(file NULL){perror(fopen failed !!\n);return -1;}char buffer[1024];// 初始化memset(buffer, 0,1024);// 赋值strcpy(buffer, content);int length strlen(buffer);// 文件写入if(fwrite(buffer, length, 1, file) 0){perror(fwrite failed !!\n);return -1;} // 文件关闭fclose(file);// 释放cJSON 对象和字符串cJSON_Delete(jsonObject);free(content);return 0;
} 编译gcc cJSON.c cjson_demo1.c -o cjson_demo1 -lm 执行./cjson_demo1 执行效果 cJSON 数据解析
新增cjson_demo2.c 文件
#includestdio.h
#includestring.h
#includestdlib.h
#include cJSON.h
/*** 目标1、引入cjson 库依赖文件(cJSON.c/cJSON.h)* 2、cjson 库基本使用 */
int main(){// 第一步从指定文件读取json 字符串FILE *file fopen(output.json, r);if(file NULL){perror(fopen failed !!\n);return -1;}char buffer[1024];// 初始化memset(buffer, 0,1024);// 文件读取fread(buffer, 1024, 1, file);// 文件关闭fclose(file);// 第二步解析json 字符串cJSON *jsonObject cJSON_Parse(buffer);if(jsonObject NULL){perror(Parse failed!\n);return -1;}// 第三步解析键值对cJSON *name cJSON_GetObjectItem(jsonObject, name);char *content cJSON_Print(name);printf(%s\n, content);// 第四步: 解析JSON对象cJSON *wife cJSON_GetObjectItem(jsonObject, wife);content cJSON_Print(wife);printf(%s\n, content);// 第五步: 解析JSON数组cJSON *childs cJSON_GetObjectItem(jsonObject, childs);content cJSON_Print(childs);printf(%s\n, content);// 释放cJSON 对象和字符串cJSON_Delete(jsonObject);free(content);return 0;
} 编译gcc cJSON.c cjson_demo2.c -o cjson_demo2 -lm 执行./cjson_demo2 执行效果: cJSON 问题
问题一找不到pow和floor函数undefined reference to pow 和 undefined reference tofloor’ 解决办法 编译需要添加math库/libm在编译代码中添加-lm。