#include <stdint.h> #define MAX_ECU 10 typedef struct { char ddd[16]; } ECU; typedef struct { char zhushini[16]; char qusi[16]; } Nishizhu; typedef struct { char sourceaddress[5]; char target_ip[16]; char local_ip[16]; char target_port[6]; ECU ecu[MAX_ECU]; Nishizhu nishizhu; } TCPSetting; typedef struct { TCPSetting tcp_setting; } Config; 结合上面的结构体定义 修改下面的函数 int loadjsonfile(char jsonfilepath) { // 读取 JSON 文件内容 FILE fp = fopen(jsonfilepath, "r"); if (fp == NULL) { printf("Failed to open file!\n"); return 1; } fseek(fp, 0, SEEK_END); long file_size = ftell(fp); rewind(fp); char* buffer = (char*)malloc(sizeof(char) * (file_size + 1)); fread(buffer, sizeof(char), file_size, fp); fclose(fp); // 解析 JSON 文件内容 json_t *root; json_error_t error; root = json_loads(buffer, 0, &error); if (!root) { printf("Error: on line %d: %s\n", error.line, error.text); return 1; } // 从 JSON 对象中获取属性值 json_t *person = json_object_get(root, "person"); json_t *name = json_object_get(person, "name"); json_t age = json_object_get(person, "age"); const char name_str = json_string_value(name); int age_int = json_integer_value(age); // 将属性值保存到结构体中 struct Person p; strcpy(p.name, name_str); p.age = age_int; // 输出结构体内容 printf("Name: %s\n", p.name); printf("Age: %d\n", p.age); // 释放 jansson 对象和缓冲区 json_decref(root); free(buffer); return 0; }
时间: 2024-03-22 16:37:40 浏览: 149
首先,需要将函数声明中的参数类型改为正确的类型,即应该是 char* jsonfilepath 而不是 char jsonfilepath。
然后,在函数中,需要根据实际情况修改结构体类型和属性名,以保证从 JSON 对象中获取的属性值能够正确地保存到对应的结构体属性中。
修改后的函数如下:
```
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "jansson.h"
#define MAX_ECU 10
typedef struct {
char ddd[16];
} ECU;
typedef struct {
char zhushini[16];
char qusi[16];
} Nishizhu;
typedef struct {
char sourceaddress[5];
char target_ip[16];
char local_ip[16];
char target_port[6];
ECU ecu[MAX_ECU];
Nishizhu nishizhu;
} TCPSetting;
typedef struct {
TCPSetting tcp_setting;
} Config;
int loadjsonfile(char* jsonfilepath) {
// 读取 JSON 文件内容
FILE* fp = fopen(jsonfilepath, "r");
if (fp == NULL) {
printf("Failed to open file!\n");
return 1;
}
fseek(fp, 0, SEEK_END);
long file_size = ftell(fp);
rewind(fp);
char* buffer = (char*)malloc(sizeof(char) * (file_size + 1));
fread(buffer, sizeof(char), file_size, fp);
fclose(fp);
// 解析 JSON 文件内容
json_t* root;
json_error_t error;
root = json_loads(buffer, 0, &error);
if (!root) {
printf("Error: on line %d: %s\n", error.line, error.text);
return 1;
}
// 从 JSON 对象中获取属性值
json_t* tcp_setting = json_object_get(root, "tcp_setting");
json_t* sourceaddress = json_object_get(tcp_setting, "sourceaddress");
json_t* target_ip = json_object_get(tcp_setting, "target_ip");
json_t* local_ip = json_object_get(tcp_setting, "local_ip");
json_t* target_port = json_object_get(tcp_setting, "target_port");
json_t* ecu = json_object_get(tcp_setting, "ecu");
json_t* nishizhu = json_object_get(tcp_setting, "nishizhu");
const char* sourceaddress_str = json_string_value(sourceaddress);
const char* target_ip_str = json_string_value(target_ip);
const char* local_ip_str = json_string_value(local_ip);
const char* target_port_str = json_string_value(target_port);
int ecu_size = json_array_size(ecu);
ECU ecu_arr[MAX_ECU];
for (int i = 0; i < ecu_size; i++) {
json_t* ecu_item = json_array_get(ecu, i);
json_t* ddd = json_object_get(ecu_item, "ddd");
const char* ddd_str = json_string_value(ddd);
strcpy(ecu_arr[i].ddd, ddd_str);
}
json_t* nishizhu_zhushini = json_object_get(nishizhu, "zhushini");
json_t* nishizhu_qusi = json_object_get(nishizhu, "qusi");
const char* nishizhu_zhushini_str = json_string_value(nishizhu_zhushini);
const char* nishizhu_qusi_str = json_string_value(nishizhu_qusi);
TCPSetting tcp_setting_obj = {
.sourceaddress = "",
.target_ip = "",
.local_ip = "",
.target_port = "",
.ecu = {0},
.nishizhu = {
.zhushini = "",
.qusi = "",
},
};
strcpy(tcp_setting_obj.sourceaddress, sourceaddress_str);
strcpy(tcp_setting_obj.target_ip, target_ip_str);
strcpy(tcp_setting_obj.local_ip, local_ip_str);
strcpy(tcp_setting_obj.target_port, target_port_str);
for (int i = 0; i < ecu_size; i++) {
tcp_setting_obj.ecu[i] = ecu_arr[i];
}
strcpy(tcp_setting_obj.nishizhu.zhushini, nishizhu_zhushini_str);
strcpy(tcp_setting_obj.nishizhu.qusi, nishizhu_qusi_str);
Config config = {
.tcp_setting = tcp_setting_obj,
};
// 输出结构体内容
printf("sourceaddress: %s\n", config.tcp_setting.sourceaddress);
printf("target_ip: %s\n", config.tcp_setting.target_ip);
printf("local_ip: %s\n", config.tcp_setting.local_ip);
printf("target_port: %s\n", config.tcp_setting.target_port);
for (int i = 0; i < ecu_size; i++) {
printf("ecu[%d].ddd: %s\n", i, config.tcp_setting.ecu[i].ddd);
}
printf("nishizhu.zhushini: %s\n", config.tcp_setting.nishizhu.zhushini);
printf("nishizhu.qusi: %s\n", config.tcp_setting.nishizhu.qusi);
// 释放 jansson 对象和缓冲区
json_decref(root);
free(buffer);
return 0;
}
```
阅读全文