Java 使用Jackson映射JSON时遇到问题

Java 使用Jackson映射JSON时遇到问题,java,json,jackson,Java,Json,Jackson,我需要映射以下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

我需要映射以下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}}]
看起来是这样的:

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

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

MyCover.java类:

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;
}

}
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;
    }

}
我曾经有过这样的想法(额外的属性不是必须的),我不知道为什么它不再有效了


那么这里可能有什么问题呢?

让我们来看看是什么原因导致了问题。如果使用提供的类运行代码,将出现以下异常:

线程“main”com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException中的异常:未识别的字段“cloudinary\u 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到属性字段的映射。将您的
封面
课程更新为:

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类不同的命名时,它是必需的。

您有什么问题?请尝试以下两个链接。在Suggestion.java和Cover.javaI中使用@JsonProperty注释我很高兴能帮助您。祝你好运我有一个与嵌套字段相关的字段,我不知道如何创建嵌套级别的POJO并从最后一个级别的
parentCategory
提取id?我想看看你能不能帮我。
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;
    }
}