当前位置:网站首页>将json文件中数组转换为struct
将json文件中数组转换为struct
2022-07-26 10:25:00 【青云 --小凡】
场景:将json文件转换为struct,json文件可能是会有嵌套,如对象中有数组,那么这个数组转为struct时应该怎么定义呢?答案是可以用结构体指针,如
typedef struct {
const char* fileLevelName;
// const char* fileName;
// const char* fileDownloadURL;
// const char* fileChecksum;
uint64_t fileSize;
// encryptionType_t encryptionType;
// const char* otaSignature;
// uint8_t flashGroup;
// downloadType_t downloadType;
// installationType_t installationType;
} fileInformation_t;
typedef struct {
// const char* ecuName;
// uint16_t ecuAddress;
// uint8_t fileNum;
fileInformation_t* fileInfoArray;
} ecuDownloadTask_t;为了简单操作,大部分都注释掉了,这是json文件
{
"OTATaskInstruction": {
"vin": "LBV8V3106GMG03526",
"taskInstructionVersion": "V0.1",
"otaTaskType": "NORMAL_TASK",
"ecuDownloadTask": [
{
"ecuName": "ABC",
"ecuAddress": "2001",
"fileInformation": [
{
"fileLevelName": "SFBL",
"fileName": "S000000012003DE.MBF",
"installationType": "AB_SELF_INSTALLATION"
},
{
"fileLevelName": "SFA1",
"fileName": "S000000008001S000000014003P.MBF",
"installationType": "AB_SELF_INSTALLATION"
}
]
},
{
"ecuName": "CDV",
"ecuAddress": "2001",
"fileInformation": [
{
"fileLevelName": "SFBL",
"fileName": "S000000012003DE.MBF",
"installationType": "AB_SELF_INSTALLATION"
},
{
"fileLevelName": "SFA1",
"fileName": "S000000008001S000000014003P.MBF",
"installationType": "AB_SELF_INSTALLATION"
}
]
}
}
}
}可以看出有ecuDownloadTask是数组,ecuDownloadTask中的元素fileInformation也是数组,那么就设计到一个ecuDownloadTask结构体中怎么去存fileInformation。按照前面结构体定义为结构体指针即可,最后一个需要解决的问题是怎么用代码实现?这里用的json-c库实现json文件到结构体转换,这篇就不介绍了,可以参考前面的文章。代码实现如下。
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <assert.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "json.h"
#include "json_tokener.h"
int main
{
ecuDownloadTask_t *ecu1 = (ecuDownloadTask_t*)malloc(sizeof(ecuDownloadTask_t));
fileInformation_t *fileinfo1 = (fileInformation_t*)malloc(sizeof(fileInformation_t));
// fileinfo1->fileLevelName = malloc(sizeof(20));
fileinfo1->fileLevelName = "downloadFile1";
fileinfo1->fileSize = 123;
ecu1[0].fileInfoArray = fileinfo1;
// printf("=====%s\n", ecu1->fileInfoArray[0].fileLevelName);
printf("=====%s\n", ecu1[0].fileInfoArray->fileLevelName);
// ecu1->fileInfoArray = fileinfo1;
// printf("%s\n", ecu1->fileInfoArray->fileLevelName);
// ecu1->fileInfoArray[0] = *fileinfo1;
// printf("=====%ld\n", ecu1->fileInfoArray[0].fileSize);
fileInformation_t *fileinfo2 = (fileInformation_t*)malloc(sizeof(fileInformation_t));
// fileinfo2->fileLevelName = malloc(sizeof(20));
fileinfo2->fileLevelName = "downloadFile2";
fileinfo2->fileSize = 312;
ecu1[1].fileInfoArray = fileinfo2;
printf("-----%s\n", ecu1[1].fileInfoArray->fileLevelName);
return 0;
}这里使用结构体数组去表示每个单独的数组结构,当然也可以使用指针加1操作,因为一个指针表示的是一整个结构体,指针加1结构体整体偏移到下一位,这里还没研究好,也就没做。
边栏推荐
- 【Halcon视觉】图像滤波
- Cause: could't make a guess for solution
- Data communication foundation - layer 2 switching principle
- 汉诺塔II|汉诺塔4柱
- 【Halcon视觉】数组
- The software cannot be opened
- On the compilation of student management system of C language course (simple version)
- Jpg to EPS
- SQL Server 2008 R2 installation problems
- 微信公众号发布提醒(微信公众号模板消息接口)
猜你喜欢
随机推荐
Vs Code configures go locale and successfully installs go related plug-ins in vscode problem: Tools failed to install
Beginner of flask framework-04-flask blueprint and code separation
Cause: couldn‘t make a guess for 解决方法
【有奖提问】向图灵奖得主、贝叶斯网络之父 Judea Pearl 提问啦
Learning about opencv (4)
Review of database -- 3. SQL language
AirTest
Li Kou daily question 917
Necessary for beginners: debug breakpoint debugging skills in idea and common breakpoint skills
string null转空字符串(空字符串是什么意思)
畅听,网文流量竞争的下一站?
数通基础-TCPIP参考模型
Flask框架初学-03-模板
Reproduce the snake game in C language (I) build pages and construct snakes
数通基础-STP原理
Strange Towers of Hanoi|汉诺塔4柱问题
Data communication foundation STP principle
简单化构造函数的继承方法(一)- 组合继承
SAP ABAP Netweaver 容器化的一些前沿性研究工作分享
关于模板函数声明与定义的问题[通俗易懂]








