非常抱歉,我之前给出的代码有一处错误。在解析 JSON 数据时,应该使用 cJSON_Parse 函数,而不是 cJSON_ParseBuffer。以下是修改后的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
int main() {
// 读取配置文件内容
FILE *file = fopen("config.json", "r");
if (file == NULL) {
printf("无法打开配置文件\n");
return 1;
}
fseek(file, 0, SEEK_END);
long file_size = ftell(file);
fseek(file, 0, SEEK_SET);
char *json_data = (char *)malloc(file_size +1);
fread(json_data, 1, file_size, file);
json_data[file_size] = '\0';
fclose(file);
// 解析 JSON 数据
cJSON *root = cJSON_Parse(json_data);
if (root == NULL) {
printf("JSON 解析错误: %s\n", cJSON_GetErrorPtr());
free(json_data);
return 1;
}
// 读取配置参数
int ttu_type = cJSON_GetObjectItem(root, "ttu_type")->valueint;
const char* sim_t1_ip = cJSON_GetObjectItem(root, "sim_t1_ip")->valuestring;
const char* sim_t2_ip = cJSON_GetObjectItem(root, "sim_t2_ip")->valuestring;
const char* sim_m_ip = cJSON_GetObjectItem(root, "sim_m_ip")->valuestring;
int single_mode_enable = cJSON_GetObjectItem(root, "single_mode_enable")->valueint;
int parallel_mode_enable = cJSON_GetObjectItem(root, "parallel_mode_enable")->valueint;
int transformer_rated_capacity = cJSON_GetObjectItem(root, "transformer_rated_capacity")->valueint;
int light_load_rate_limit = cJSON_GetObjectItem(root, "light_load_rate_limit")->valueint;
int heavy_load_rate_limit = cJSON_GetObjectItem(root, "heavy_load_rate_limit")->valueint;
int no_voltage_limit = cJSON_GetObjectItem(root, "no_voltage_limit")->valueint;
int no_voltage_phases = cJSON_GetObjectItem(root, "no_voltage_phases")->valueint;
int single_mode_closing_delay = cJSON_GetObjectItem(root, "single_mode_closing_delay")->valueint;
int parallel_mode_closing_delay = cJSON_GetObjectItem(root, "parallel_mode_closing_delay")->valueint;
int single_mode_opening_delay = cJSON_GetObjectItem(root, "single_mode_opening_delay")->valueint;
int ct_ratio = cJSON_GetObjectItem(root, "ct_ratio")->valueint;
int voltage_difference_judgment_enable = cJSON_GetObjectItem(root, "voltage_difference_judgment_enable")->valueint;
int phase_difference_judgment_enable = cJSON_GetObjectItem(root, "phase_difference_judgment_enable")->valueint;
int allowable_voltage_difference_for_closing = cJSON_GetObjectItem(root, "allowable_voltage_difference_for_closing")->valueint;
int allowable_phase_difference_for_closing = cJSON_GetObjectItem(root, "allowable_phase_difference_for_closing")->valueint;
// 使用配置参数
printf("ttu_type: %d\n", ttu_type);
printf("sim_t1_ip: %s\n", sim_t1_ip);
printf("sim_t2_ip: %s\n", sim_t2_ip);
printf("sim_m_ip: %s\n", sim_m_ip);
printf("single_mode_enable: %d\n", single_mode_enable);
printf("parallel_mode_enable: %d\n", parallel_mode_enable);
printf("transformer_rated_capacity: %d\n", transformer_rated_capacity);
printf("light_load_rate_limit: %d\n", light_load_rate_limit);
printf("heavy_load_rate_limit: %d\n", heavy_load_rate_limit);
printf("no_voltage_limit: %d\n", no_voltage_limit);
printf("no_voltage_phases: %d\n", no_voltage_phases);
printf("single_mode_closing_delay: %d\n", single_mode_closing_delay);
printf("parallel_mode_closing_delay: %d\n", parallel_mode_closing_delay);
printf("single_mode_opening_delay: %d\n", single_mode_opening_delay);
printf("ct_ratio: %d\n", ct_ratio);
printf("voltage_difference_judgment_enable: %d\n", voltage_difference_judgment_enable);
printf("phase_difference_judgment_enable: %d\n", phase_difference_judgment_enable);
printf("allowable_voltage_difference_for_closing: %d\n", allowable_voltage_difference_for_closing);
printf("allowable_phase_difference_for_closing: %d\n", allowable_phase_difference_for_closing);
// 释放资源
cJSON_Delete(root);
free(json_data);
return 0;
}
```
确保在编译时链接 cJSON 库,并将修改后的配置文件内容保存为 "config.json"。然后,编译并运行该程序。它将加载并解析配置文件,并将配置参数打印到控制台上。
如果你有任何进一步的问题,请随时提问!