gpt4 book ai didi

java - 使用 Jackson 映射 JSON 时遇到问题

转载 作者:行者123 更新时间:2023-11-30 02:27:42 25 4
gpt4 key购买 nike

我需要映射以下 JSON:

[{"id":7346,"name":"The Legend of Zelda: Breath of the Wild","cover":{"url":"//images.igdb.com/igdb/image/upload/t_thumb/jk9el4ksl4c7qwaex2y5.jpg","cloudinary_id":"jk9el4ksl4c7qwaex2y5","width":2709,"height":3816}},{"id":2909,"name":"The Legend of Zelda: A Link Between Worlds","cover":{"url":"//images.igdb.com/igdb/image/upload/t_thumb/r9ezsk5yhljc83dfjeqc.jpg","cloudinary_id":"r9ezsk5yhljc83dfjeqc","width":406,"height":362}}]

它看起来像这样: JSON

当我不需要“cover”字段时(我的域类只有 ID 和名称),映射它没有任何问题。现在,当我需要它时我也遇到了问题。

这是我用来将 JSON 映射为字符串的代码行(就像我说的,如果我只坚持 id 和 name 字段,它就可以正常工作)。

List<Suggestion> suggestions = mapper.readValue(requestString, new TypeReference<List<Suggestion>>(){});

我的Suggestion.java类(class):

public class Suggestion {

private long id;
private String name;
private Cover cover;

public Suggestion(){
}

public Suggestion(long id, String name, Cover cover) {
this.id = id;
this.name = name;
this.cover = cover;
}

public long getId() {
return id;
}

public String getName() {
return name;
}

public Cover getCover() {
return cover;
}

}

我的Cover.java类:

public class Cover {
private String url;
private String cloudinaryId;
private Integer width;
private Integer height;

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getCloudinaryId() {
return cloudinaryId;
}

public void setCloudinaryId(String cloudinaryId) {
this.cloudinaryId = cloudinaryId;
}

public Integer getWidth() {
return width;
}

public void setWidth(Integer width) {
this.width = width;
}

public Integer getHeight() {
return height;
}

public void setHeight(Integer height) {
this.height = height;
}

}

我用过http://www.jsonschema2pojo.org/得到这个想法(附加属性肯定不是必需的),我不确定为什么它不再起作用。

那么这里可能存在什么问题呢?

最佳答案

让我们检查一下导致问题的原因。如果您使用您提供的类运行代码,您将收到以下异常:

Exception in thread "main" com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "cloudinary_id"

示例中的 JSON 如下:

[
{
"id": 7346,
"name": "The Legend of Zelda: Breath of the Wild",
"cover": {
"url": "//images.igdb.com/igdb/image/upload/t_thumb/jk9el4ksl4c7qwaex2y5.jpg",
"cloudinary_id": "jk9el4ksl4c7qwaex2y5",
"width": 2709,
"height": 3816
}
},
{
"id": 2909,
"name": "The Legend of Zelda: A Link Between Worlds",
"cover": {
"url": "//images.igdb.com/igdb/image/upload/t_thumb/r9ezsk5yhljc83dfjeqc.jpg",
"cloudinary_id": "r9ezsk5yhljc83dfjeqc",
"width": 406,
"height": 362
}
}
]

您错过的是使用 @JsonProperty 注释指定 JSON 到属性字段映射。将您的 Cover 类更新为:

public class Cover {

private String url;
@JsonProperty("cloudinary_id")
private String cloudinaryId;
private Integer width;
private Integer height;

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}

public String getCloudinaryId() {
return cloudinaryId;
}

public void setCloudinaryId(String cloudinaryId) {
this.cloudinaryId = cloudinaryId;
}

public Integer getWidth() {
return width;
}

public void setWidth(Integer width) {
this.width = width;
}

public Integer getHeight() {
return height;
}

public void setHeight(Integer height) {
this.height = height;
}
}

当 JSON 字段 1:1 映射到类字段名称时,您不必使用此注释。当 JSON 使用与 Java 类不同的命名时,这是必需的。

关于java - 使用 Jackson 映射 JSON 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45215081/

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