gpt4 book ai didi

java - Jackson Json 到 POJO 的映射

转载 作者:行者123 更新时间:2023-11-30 06:48:27 26 4
gpt4 key购买 nike

我在与 jackson 创建映射时有点迷失。我的 Json 具有以下结构

    {
"d": {
"__metadata": {
"uri": "https://apisalesdemo8.successfactors.com:443/odata/v2/JobApplication(1463L)",
"type": "SFOData.JobApplication"
},
"lastName": "K",
"address": "123 Main Street",
"cellPhone": "12345",
"firstName": "Katrin",
"city": "Anytown",
"country": "United States",
"custappattachment": {
"results": [
{
"__metadata": {
"uri": "https://apisalesdemo8.successfactors.com:443/odata/v2/Attachment(1188L)",
"type": "SFOData.Attachment"
},
"fileExtension": "jpeg",
"fileName": "hp-hero-img.jpeg",
"fileContent": "/9j/4AA"
},
{
"__metadata": {
"uri": "https://apisalesdemo8.successfactors.com:443/odata/v2/Attachment(1189L)",
"type": "SFOData.Attachment"
},
"fileExtension": "jpeg",
"fileName": "hp-content-bkgd-img.jpeg",
"fileContent": "/9j/4AAQSk"
}]}}}

我确实找到了很多处理数组的教程,但我在第一个标记“d”时就失败了。并且根本不需要所有“__metadata” token 。

我创建了一个 pojo,其中包含诸如姓氏等属性和一个集合附件。但我的代码总是在标记“d”或“__metadata”处失败

public class ResponseDataObject {


private String lastName;
private String address;
private String cellPhone;
private String firstName;
private String city;
private String country;
private List<Attachment> attachments = new ArrayList<>();
.....getters and setters

和 jackson 阅读器

    ObjectReader objectReader =
mapper.readerFor(ResponseDataObject.class);
ResponseDataObject dataObject = objectReader.readValue(file);

如有任何提示,我们将不胜感激。

问候马蒂亚斯

最佳答案

您可以使用@JsonIgnoreProperties注释来忽略与类中定义的任何成员不匹配的属性,例如:

@JsonIgnoreProperties(ignoreUnknown = true)
public class ResponseDataObject {

这将确保映射所有匹配的属性并忽略其他属性。

关于java - Jackson Json 到 POJO 的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43274512/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com