如何从对象中提取列表类型的属性内的json数组

吉安卡洛·阿贝尔·朱利安(Giancarlo Abel Giulian)

我正在使用Flickr API来获取图像信息并返回以下JSON:

{“照片”:{“页面”:1,“页面”:60,“每页”:100,“总计”:“ 5964”,“照片”:[{“ id”:“ 21577339501”,“所有者”: “ 85277110 @ N02”,“秘密”:“ 31e850dfeb”,“服务器”:“ 5785”,“农场”:6,“标题”:“ P1390956”,“ ispublic”:1,“ isfriend”:0,“ isfamily “:0},{” id“:” 21577287101“,”所有者“:” 85277110 @ N02“,”秘密“:” 412990658f“,”服务器“:” 611“,”农场“:1,”标题“: “ P1400012”,“ ispublic”:1,“ isfriend”:0,“ isfamily”:0}]

我在Spring控制器中使用以下代码反序列化JSON:

Collection<Photos> readValues = objectMapper.readValue(new URL(url), new TypeReference<Collection<Photos>>() { });

并返回以下错误:

com.fasterxml.jackson.databind.JsonMappingException:无法从START_OBJECT令牌中反序列化java.util.ArrayList实例

我怎么解决这个问题?我没有找到解决方案。

照片类:

public class Photos {

    @JsonProperty("page")
    private Integer page;

    @JsonProperty("pages")
    private Integer pages;

    @JsonProperty("perpage")
    private Integer perpage;

    @JsonProperty("total")
    private Integer total;

    @JsonProperty("photo")
    @JsonDeserialize(contentAs = Photo.class, as = ArrayList.class)
    private List<Photo> photo;

    public Photos() {}

    public Photos(Integer page, Integer pages, Integer perpage, Integer total,
            List<Photo> photo) {
        super();
        this.page = page;
        this.pages = pages;
        this.perpage = perpage;
        this.total = total;
        this.photo = photo;
    }

    public Photos(List<Photo> photo) {
        super();
        this.photo = photo;
    }

    public Integer getPage() {
        return page;
    }
    public void setPage(Integer page) {
        this.page = page;
    }
    public Integer getPages() {
        return pages;
    }
    public void setPages(Integer pages) {
        this.pages = pages;
    }
    public Integer getPerpage() {
        return perpage;
    }
    public void setPerpage(Integer perpage) {
        this.perpage = perpage;
    }
    public Integer getTotal() {
        return total;
    }
    public void setTotal(Integer total) {
        this.total = total;
    }
    public List<Photo> getPhoto() {
        return photo;
    }

    public void setPhoto(List<Photo> photo) {
        this.photo = photo;
    }
}

照片类:

public class Photo {

    @JsonProperty("id")
    private Integer id;

    @JsonProperty("owner")
    private String owner;

    @JsonProperty("secret")
    private String secret;

    @JsonProperty("server")
    private Integer server;

    @JsonProperty("farm")
    private Integer farm;

    @JsonProperty("title")
    private String title;

    @JsonProperty("ispublic")
    private Boolean isPublic;

    @JsonProperty("isfriend")
    private Boolean isFriend;

    @JsonProperty("isfamily")
    private Boolean isFamily;

    public Photo() { }

    public Photo(Integer id, String owner, String secret, Integer server,
            Integer farm, String title, Boolean isPublic, Boolean isFriend,
            Boolean isFamily) {
        super();
        this.id = id;
        this.owner = owner;
        this.secret = secret;
        this.server = server;
        this.farm = farm;
        this.title = title;
        this.isPublic = isPublic;
        this.isFriend = isFriend;
        this.isFamily = isFamily;
    }
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getOwner() {
        return owner;
    }
    public void setOwner(String owner) {
        this.owner = owner;
    }
    public String getSecret() {
        return secret;
    }
    public void setSecret(String secret) {
        this.secret = secret;
    }
    public Integer getServer() {
        return server;
    }
    public void setServer(Integer server) {
        this.server = server;
    }
    public Integer getFarm() {
        return farm;
    }
    public void setFarm(Integer farm) {
        this.farm = farm;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public Boolean getIsPublic() {
        return isPublic;
    }
    public void setIsPublic(Boolean isPublic) {
        this.isPublic = isPublic;
    }
    public Boolean getIsFriend() {
        return isFriend;
    }
    public void setIsFriend(Boolean isFriend) {
        this.isFriend = isFriend;
    }
    public Boolean getIsFamily() {
        return isFamily;
    }
    public void setIsFamily(Boolean isFamily) {
        this.isFamily = isFamily;
    }

}
波希米亚风格

基本的问题是您的json不是a Collection<Photos>,而是a Map<String, Photos>,它只有一个条目"photos" -> Photos instance

通过进行以下更改,我使您的json成功反序列化了...

A)更改要读取的类型:

Map<String, Photos> readValues = objectMapper.readValue(json, new TypeReference<Map<String, Photos>>() { });

请注意,我直接从字符串(而不是URL)读取。

B)将Photo.idfrom的类型更改IntegerLong,因为您的json的ID值远远超过最大int大小。

C)我在示例json中添加了缺少的两个右括号,以使其有效。

仅供参考,反序列化可以@JsonDeserialize在的List<Photo> photo字段中添加或不添加注释Photos


这是一些可以运行的代码:

String json = "{\"photos\":{\"page\":1,\"pages\":60,\"perpage\":100,\"total\":\"5964\",\"photo\":[{\"id\":\"21577339501\",\"owner\":\"85277110@N02\",\"secret\":\"31e850dfeb\",\"server\":\"5785\",\"farm\":6,\"title\":\"P1390956\",\"ispublic\":1,\"isfriend\":0,\"isfamily\":0}, {\"id\":\"21577287101\",\"owner\":\"85277110@N02\",\"secret\":\"412990658f\",\"server\":\"611\",\"farm\":1,\"title\":\"P1400012\",\"ispublic\":1,\"isfriend\":0,\"isfamily\":0}]}}"; 
Map<String, Photos> readValues = new ObjectMapper().readValue(json, new TypeReference<Map<String, Photos>>() { });

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章