推荐cJSON下载地址:
Git地址:https://github.com/arnoldlu/cJSON
可以尝试:sudo apt-get install libcjson-dev,笔者未使用该方式。
百度AI搜索程序,供参考,部分函数名称应以cJSON模块内定义为主
json文件读写
#include <stdio.h>
#include <stdlib.h>
#include "cjson/cJSON.h"void write_json(char *filename) {// 创建一个JSON对象cJSON *json = cJSON_CreateObject();cJSON_AddStringToObject(json, "name", "John Doe");cJSON_AddNumberToObject(json, "age", 30);// 将JSON对象转换成字符串char *json_string = cJSON_Print(json);// 打开文件FILE *file = fopen(filename, "w");if (file) {// 写入JSON字符串到文件fprintf(file, "%s", json_string);fclose(file);}// 清理cJSON_Delete(json);free(json_string);
}cJSON *read_json(char *filename) {// 读取文件内容到字符串FILE *file = fopen(filename, "r");if (file == NULL) {return NULL;}fseek(file, 0, SEEK_END);long length = ftell(file);fseek(file, 0, SEEK_SET);char *data = (char *)malloc(length + 1);fread(data, 1, length, file);data[length] = '\0';fclose(file);// 解析JSON字符串cJSON *json = cJSON_Parse(data);free(data);return json;
}int main() {char *filename = "example.json";// 写入JSON文件write_json(filename);// 读取JSON文件cJSON *json = read_json(filename);if (json) {// 获取并打印JSON字段const cJSON *name = cJSON_GetObjectItemCaseSensitive(json, "name");const cJSON *age = cJSON_GetObjectItemCaseSensitive(json, "age");if (cJSON_IsString(name) && (name->valuestring != NULL) && cJSON_IsNumber(age)) {printf("Name: %s\n", name->valuestring);printf("Age: %d\n", age->valueint);}cJSON_Delete(json);}return 0;
}
注意 :上述示例代码中,函数cJSON_GetObjectItemCaseSensitive()和函数cJSON_IsString()不存在,请以 cJSON_GetObjectItem()和 type属性代替之。下同。
数组使用
#include <stdio.h>
#include <stdlib.h>
#include "cjson/cJSON.h"int main() {// 创建一个包含数组的 JSON 对象cJSON *json = cJSON_CreateObject();cJSON *array = cJSON_CreateIntArray(NULL, 0);// 向数组中添加元素int numbers[] = {1, 2, 3, 4, 5};cJSON_AddItemToArray(array, cJSON_CreateIntArray(numbers, sizeof(numbers)/sizeof(numbers[0])));// 将数组添加到 JSON 对象cJSON_AddItemToObject(json, "numbers", array);// 打印 JSON 对象char *jsonString = cJSON_Print(json);printf("%s\n", jsonString);// 获取并打印数组的大小int arraySize = cJSON_GetArraySize(cJSON_GetObjectItemCaseSensitive(json, "numbers"));printf("The size of the array is: %d\n", arraySize);// 清理 JSON 对象cJSON_Delete(json);free(jsonString);return 0;
}
工程实际测试实例:
文件:pwr_mngr_cfg.json
{"Device Path" : "/sys/class/device","Channel Index" : [1,2,3,4,5,6,7,8,9,10,27,28,29,30,31,32,43,44,45,52,53,54,55,56,57,58,63,64,65,66]
}
json配置文件的读写 :
全局变量:uint8_t PwrChnlIdxCfg[80];
代码:
FILE *fp_JsonCfg = fopen("./pwr_mngr_cfg.json", "r"); /* */if (fp_JsonCfg == NULL){perror("fopen(pwr_mngr_cfg.json) fail");exit(EXIT_FAILURE);}fseek(fp_JsonCfg, 0, SEEK_END);long json_obj_size = ftell(fp_JsonCfg);fseek(fp_JsonCfg, 0, SEEK_SET);char *pc_json_data = (char *)malloc(json_obj_size + 1);cJSON *json_root = NULL;char dev_name[128U];memset(dev_name, '\0', sizeof(dev_name));memset(PwrChnlIdxCfg, 0x00, sizeof(PwrChnlIdxRgt));if(pc_json_data != NULL){fread(pc_json_data, 1, json_obj_size, fp_JsonCfg);pc_json_data[json_obj_size] = '\0';json_root = cJSON_Parse(pc_json_data);if(json_root != NULL){const cJSON *dev_path = cJSON_GetObjectItem(json_root, "Device Path");if((dev_path->type == cJSON_String) && (dev_path->valuestring != NULL)){strcpy(dev_name, dev_path->valuestring);printf("Device Path: %s\n", dev_name);}const cJSON *chnl_idx = cJSON_GetObjectItem(json_root, "Channel Index");if (chnl_idx->type == cJSON_Array){int n_sz = cJSON_GetArraySize(chnl_idx);printf("Total Channel Index number is : %d\n", n_sz);for(int idx = 0; idx < n_sz; ++idx){const cJSON *cur_item = cJSON_GetArrayItem(chnl_idx, idx);if(cur_item->type == cJSON_Number){PwrChnlIdxCfg[idx] = cur_item->valueint;printf("%d ", PwrChnlIdxCfg[idx]);}}printf("\n");}}cJSON_Delete(json_root);}fclose(fp_JsonCfg);free(pc_json_data);
测试结果:
(完)