java - 使用 Jackson 如何在 JSON 包含对象列表时将其映射到对象
问题描述
这是我的 JSON 有效负载
{
"allEnvs": ["qa", "dev", "prestaging"],
"env": "qa",
"envUrls": [{
"qa": {
"cutomeUrl": "testUrl",
"adminUrl": "",
"webUrl": "https://test.try.val",
"salesUrl": ""
},
"dev": {
"cutomeUrl": "testUrl",
"webUrl": "",
"salesUrl": ""
},
"prestaging": {
"cutomeUrl": "testUrl",
"webUrl": "",
"salesUrl": ""
}
}],
"isCommonUsers": "true",
"commonUsers":[ {
"teacher": {
"fName": "test",
"lName": "test"
},
"student": {
"fName": "test",
"lName": "test"
},
"ta": {
"fName": "test",
"lName": "test"
}
}],
"commonCodes": ["test1", "test2", "test3"]
}
我想知道如何为我的“Conf.class”映射“envUrls”、“commonUsers”
ObjectMapper mapper = new ObjectMapper();
CommonConf cnf = mapper.readValue( new File("src/main/resources/configs/config.json"), Conf.class);
解决方案
您可以Conf
使用以下内容注释您的课程:
@JsonIgnoreProperties(ignoreUnknown = true)
public class Conf {
...
}
它在解析 JSON 时忽略未知属性
推荐阅读
- java - 与类中的私有构造函数声明相关的问题
- scala - SBT 插件 %%% 在哪里定义?
- swift - 关于 Swift Api 调用
- python - 7 使用带有网络摄像头的 OpenCV 进行分段检测
- postgresql - Apache Superset:从 sqlite 迁移到 Postgres
- javascript - 为字符串中以逗号分隔的每个值创建新数组
- python-3.x - Google Kickstart 2020 Round E RuntimeError
- android - 安装新apk时自动清理以前的数据和缓存
- javascript - 根据选择框值更新 HTML 数据列表 - Google Apps/javascript
- c - 如何在以下代码中添加 Y/N?