当前位置:网站首页>json-c实现json和结构体之间的相互转换
json-c实现json和结构体之间的相互转换
2022-07-29 19:59:00 【青云 --小凡】
代码:
#ifdef NDEBUG
#undef NDEBUG
#endif
#include "strerror_override.h"
#include "strerror_override_private.h"
#include <fcntl.h>
#include <stddef.h>
#include <stdlib.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <assert.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include <pthread.h>
// #include "json.h"
#include "json_util.h"
#include "snprintf_compat.h"
#include "json_visit.h"
#include "json_c_version.h"
#include "json_types.h"
#ifndef PATH_MAX
#define PATH_MAX 256
#endif
typedef enum {
ONGOING,
COMPLETE,
} downloadStatus_t;
typedef struct {
const char* ecuName;
uint16_t ecuAddress;
const char* fileName;
uint8_t downloadTimes;
uint64_t receiveFileSize;
uint64_t totalFileSize;
// downloadStatus_t downloadStatus;
} downloadProgressReportSingle_t;
typedef struct {
const char* otaTaskUUID;
uint8_t repNum;
downloadProgressReportSingle_t *repArray;
} downloadProgressReport_t;
downloadProgressReport_t* dp_rep;
downloadProgressReportSingle_t* rep_array;
static json_object* read_valid_nested_with_fd(const char *testdir);
static downloadProgressReportSingle_t file_info_json_data_to_struct(json_object* every_one_ecu, int elm);
static json_object* struct_data_to_json(void);
int main(void)
{
const char *srcfile = "/home/zyf/sourceOpen/json-c/tests";
// const char *srcfile = "./";
if (strncmp(json_c_version(), JSON_C_VERSION, sizeof(JSON_C_VERSION)))
{
printf("FAIL: Output from json_c_version(): %s "
"does not match %s",
json_c_version(), JSON_C_VERSION);
return EXIT_FAILURE;
}
if (json_c_version_num() != JSON_C_VERSION_NUM)
{
printf("FAIL: Output from json_c_version_num(): %d "
"does not match %d",
json_c_version_num(), JSON_C_VERSION_NUM);
return EXIT_FAILURE;
}
struct json_object* src_json = read_valid_nested_with_fd(srcfile);
printf("%s\n", json_object_to_json_string_ext(src_json, JSON_C_TO_STRING_PRETTY));
dp_rep = (downloadProgressReport_t*)malloc(sizeof(downloadProgressReport_t) * 9);
memset(dp_rep, 0, sizeof(downloadProgressReport_t)*9);
dp_rep->repArray = malloc(sizeof(*dp_rep->repArray) * 3);
memset(dp_rep->repArray, 0, sizeof(downloadProgressReportSingle_t)*3);
// rep_array = (downloadProgressReportSingle_t*)malloc(sizeof(downloadProgressReportSingle_t));
struct json_object* root_json;
json_object_object_get_ex(src_json, "otaTaskUUID", &root_json);
if(json_object_get_type(root_json) == json_type_string)
{
dp_rep->otaTaskUUID = json_object_get_string(root_json);
}
json_object_put(root_json);
json_object_object_get_ex(src_json, "progressSequence", &root_json);
if(json_object_get_type(root_json) == json_type_array)
{
size_t length = json_object_array_length(root_json);
for(size_t index=0; index < length; index++)
{
// printf("--------------------index=%ld------------------\n", index);
// struct json_object* array_json = json_object_array_get_idx(root_json, index);
downloadProgressReportSingle_t dprs;
dprs = file_info_json_data_to_struct(root_json, index);
// rep_array = &dprs;
// dp_rep[index].repArray = rep_array;
// dp_rep[index].repArray = &dprs;
dp_rep->repArray[index].downloadTimes = dprs.downloadTimes;
dp_rep->repArray[index].ecuAddress = dprs.ecuAddress;
dp_rep->repArray[index].ecuName = dprs.ecuName;
dp_rep->repArray[index].fileName = dprs.fileName;
dp_rep->repArray[index].receiveFileSize = dprs.receiveFileSize;
dp_rep->repArray[index].totalFileSize = dprs.totalFileSize;
// *(dp_rep->repArray) = dprs;
// dp_rep->repArray->ecuName = dprs.ecuName;
// printf("-ecuName=%s\n", dp_rep->repArray->ecuName);
// printf("ecuName=%s\n", dprs.ecuName);
// printf("index=%ld---%s----%d----%d----%s----%s----%ld----%ld\n\n", index,
// dp_rep->otaTaskUUID,
// // dp_rep[index].repArray->downloadStatus,
// dp_rep[index].repArray->downloadTimes,
// dp_rep[index].repArray->ecuAddress,
// dp_rep[index].repArray->ecuName,
// dp_rep[index].repArray->fileName,
// dp_rep[index].repArray->receiveFileSize,
// dp_rep[index].repArray->totalFileSize
// );
}
for(size_t index = 0; index < 3; ++index)
{
// printf("--ecuName=%s\n", dp_rep->repArray[index].ecuName);
printf("index=%ld---%s----%d----%d----%s----%s----%ld----%ld\n\n", index,
dp_rep->otaTaskUUID,
// dp_rep->repArray[index].downloadStatus,
dp_rep->repArray[index].downloadTimes,
dp_rep->repArray[index].ecuAddress,
dp_rep->repArray[index].ecuName,
dp_rep->repArray[index].fileName,
dp_rep->repArray[index].receiveFileSize,
dp_rep->repArray[index].totalFileSize
);
}
}
struct json_object* str_root_json = struct_data_to_json();
printf("\n\n\n%s\n", json_object_to_json_string_ext(str_root_json, JSON_C_TO_STRING_PRETTY));
return 0;
}
static json_object* struct_data_to_json()
{
// 结构体转json结构
struct json_object* str_root_json = json_object_new_object();
json_object_object_add(str_root_json, "otaTaskUUID" , json_object_new_string(dp_rep->otaTaskUUID));
struct json_object* temp_json = json_object_new_array();
for(size_t index = 0; index < 3; ++index)
{
struct json_object* str_array_json = json_object_new_object();
json_object_object_add(str_array_json, "ecuName", json_object_new_string(dp_rep->repArray[index].ecuName));
json_object_object_add(str_array_json, "ecuAddress", json_object_new_int(dp_rep->repArray[index].ecuAddress));
json_object_object_add(str_array_json, "fileName", json_object_new_string(dp_rep->repArray[index].fileName));
json_object_object_add(str_array_json, "downloadTimes", json_object_new_int(dp_rep->repArray[index].downloadTimes));
json_object_object_add(str_array_json, "receiveFileSize", json_object_new_int64(dp_rep->repArray[index].receiveFileSize));
json_object_object_add(str_array_json, "totalFileSize", json_object_new_int64(dp_rep->repArray[index].totalFileSize));
json_object_array_add(temp_json, str_array_json);
}
json_object_object_add(str_root_json, "progressSequence", temp_json);
return str_root_json;
}
// #define CHECK_PRINTF
static downloadProgressReportSingle_t file_info_json_data_to_struct(json_object* every_one_ecu, int elm)
{
#ifdef CHECK_PRINTF
printf("----------------file_info_json_data_to_struct------begin[[[....\n");
#endif
downloadProgressReportSingle_t *file_info_struct = malloc(sizeof(downloadProgressReportSingle_t));
struct json_object* file_info_array_json = json_object_array_get_idx(every_one_ecu, elm);
struct json_object* final_json;
json_object_object_get_ex(file_info_array_json, "ecuName", &final_json);
if(json_object_get_type(final_json) == json_type_string)
{
file_info_struct->ecuName = json_object_get_string(final_json);
#ifdef CHECK_PRINTF
printf("%s\n", file_info_struct->ecuName);
#endif
}
json_object_put(final_json);
json_object_object_get_ex(file_info_array_json, "ecuAddress", &final_json);
if(json_object_get_type(final_json) == json_type_int)
{
file_info_struct->ecuAddress = json_object_get_int(final_json);
#ifdef CHECK_PRINTF
printf("%d\n", file_info_struct->ecuAddress);
#endif
}
json_object_put(final_json);
json_object_object_get_ex(file_info_array_json, "fileName", &final_json);
if(json_object_get_type(final_json) == json_type_string)
{
file_info_struct->fileName = json_object_get_string(final_json);
#ifdef CHECK_PRINTF
printf("%s\n", file_info_struct->fileName);
#endif
}
json_object_put(final_json);
json_object_object_get_ex(file_info_array_json, "downloadTimes", &final_json);
if(json_object_get_type(final_json) == json_type_int)
{
file_info_struct->downloadTimes = json_object_get_int(final_json);
#ifdef CHECK_PRINTF
printf("%d\n", file_info_struct->downloadTimes);
#endif
}
json_object_put(final_json);
json_object_object_get_ex(file_info_array_json, "receiveFileSize", &final_json);
if(json_object_get_type(final_json) == json_type_int)
{
file_info_struct->receiveFileSize = json_object_get_int(final_json);
#ifdef CHECK_PRINTF
printf("%ld\n", file_info_struct->receiveFileSize);
#endif
}
json_object_put(final_json);
json_object_object_get_ex(file_info_array_json, "totalFileSize", &final_json);
if(json_object_get_type(final_json) == json_type_int)
{
file_info_struct->totalFileSize = json_object_get_int(final_json);
#ifdef CHECK_PRINTF
printf("%ld\n", file_info_struct->totalFileSize);
#endif
}
json_object_put(final_json);
// json_object_object_get_ex(file_info_array_json, "downloadStatus", &final_json);
// if(json_object_get_type(final_json) == json_type_string)
// {
// file_info_struct->downloadStatus = COMPLETE;//json_object_get_string(final_json);
// #ifdef CHECK_PRINTF
// printf("%d\n", file_info_struct->downloadStatus);
// #endif
// }
// json_object_put(final_json);
#ifdef CHECK_PRINTF
printf("----------------file_info_json_data_to_struct------end]]]]\n");
#endif
return *file_info_struct;
}
static json_object* read_valid_nested_with_fd(const char *testdir)
{
char filename[PATH_MAX];
// (void)snprintf(filename, sizeof(filename), "%s/valid_nested.json", testdir);
(void)snprintf(filename, sizeof(filename), "%s/OTADownloadProgress.json", testdir);
int fd = open(filename, O_RDONLY, 0);
printf("filename=%s\n", filename);
if (fd < 0)
{
// printf(stderr, "FAIL: unable to open %s: %s\n", filename, strerror(errno));
exit(EXIT_FAILURE);
}
assert(NULL == json_object_from_fd_ex(fd, -2));
json_object *jso = json_object_from_fd_ex(fd, 20);
if (jso != NULL)
{
// no thing
}
else
{
// printf(stderr, "FAIL: unable to parse contents of %s: %s\n", filename,
// json_util_get_last_err());
}
close(fd);
return jso;
}json文件:
{
"otaTaskUUID": "85D7F5310717AE3A108AC5E8A3922E22",
"repNum": 3,
"progressSequence": [
{
"ecuName": "TBOX",
"ecuAddress": 4113,
"fileName": "S000000012003DE.MBF",
"downloadTimes": 1,
"receiveFileSize": 102,
"totalFileSize": 10238869,
"downloadStatus": "ONGOING"
},
{
"ecuName": "DCD",
"ecuAddress": 12289 ,
"fileName": "S000000012003DE.MBF",
"downloadTimes": 1,
"receiveFileSize": 102,
"totalFileSize": 10238869,
"downloadStatus": "ONGOING"
},
{
"ecuName": "ADD",
"ecuAddress": 8193,
"fileName": "S000000012003DE.MBF",
"downloadTimes": 1,
"receiveFileSize": 102,
"totalFileSize": 10238869,
"downloadStatus": "ONGOING"
}
]
}边栏推荐
- [数学基础]线性代数相关概念学习
- [mathematical foundation] probability and mathematical statistics related concept learning
- 用对象字面量或Map替代Switch/if语句
- Internship: use easypoi to import and export excel table data
- 这半年我做交易链路自动化回归的那些事儿...
- [GXYCTF2019]禁止套娃
- 单核浏览器和双核浏览器有什么区别,哪个好用?
- easyExce模板填充生成Excel的实际操作,多sheet页处理
- 2022中国物流产业大会暨企业家高峰论坛在杭州举办!
- JMeter使用教程(一)
猜你喜欢

Omni-channel e-commerce | How can well-known domestic cosmeceuticals seize the opportunity to achieve rapid growth?

正则表达式

RNA的化学修饰原理|Gal-PEG-siRNA|siRNA-S-S-DSPE|siRNA-s-s-PEG|cholesterol-siRNA

7 行代码搞崩溃 B 站,原因令人唏嘘!

图床软件要收费,算了我自己写一个开源免费的。
![[数学基础]概率论与数理统计相关概念学习](/img/bc/d3a246240ff7aca2b84c3766383758.png)
[数学基础]概率论与数理统计相关概念学习

Private domain growth | Private domain members: 15 case collections from 9 major chain industries

Single-core browser and what is the difference between dual-core browser, which to use?

第二增长曲线 | 企业创新突破停滞困境的驱动指南

模拟量、数字量与开关量的区别
随机推荐
[GXYCTF2019]禁止套娃
Detailed explanation of design ideas of webUI test framework
[mathematical foundation] probability and mathematical statistics related concept learning
EasyExce template filling generation of Excel of actual operation, many processing sheet page
荣耀的野望:智慧世界的“高端平民”
Unity determines whether a string can be converted to float type
Is it safe to use the MD5 encrypted string to store the password?Hash algorithm you have to know
Durable rules(持久规则引擎) 学习小记
ESP8266-Arduino programming example-I2C device address scan
悲伤的摇滚乐
如何进入董事会:给CIO的十条建议
There is a fee for the picture bed software. Forget it, I wrote an open source free one.
震荡波病毒原代码(勒索病毒源代码)
模拟量、数字量与开关量的区别
Expert advice | How to formulate a growth strategy for survival in an economic downturn
尿素偶联Urea-siRNA Conjugates|Cyclodextrin-siRNA-β-CD环糊精修饰RNA核酸(解析说明)
internship:利用easypoi将excel表数据导入导出
全渠道电商 | 国内知名的药妆要如何抓住风口实现快速增长?
关于安装软件时x86 ,x64,x86_64,ARM 64, ARM 32 的选择
ESP8266-Arduino programming example-EEPROM read and write