How to turn this JSON response into POJO?

。_饼干妹妹 提交于 2019-12-04 22:44:57

Ideally, the list property in the JSON object should actually be an array of elements instead of inner JSON objects. However, you could use the following for modelling your data.

import org.codehaus.jackson.annotate.JsonProperty;

import java.util.Map;

class POJO {

    private Integer status;
    private Integer complete;
    private String error;
    private Long since;

    @JsonProperty("search_meta")
    private SearchMeta searchMeta;

    private Map<String, Item> list;

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Integer getComplete() {
        return complete;
    }

    public void setComplete(Integer complete) {
        this.complete = complete;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public Long getSince() {
        return since;
    }

    public void setSince(Long since) {
        this.since = since;
    }

    public SearchMeta getSearchMeta() {
        return searchMeta;
    }

    public void setSearchMeta(SearchMeta searchMeta) {
        this.searchMeta = searchMeta;
    }

    public Map<String, Item> getList() {
        return list;
    }

    public void setList(Map<String, Item> list) {
        this.list = list;
    }
}

class SearchMeta {

    @JsonProperty("search_type")
    private String searchType;

    public String getSearchType() {
        return searchType;
    }

    public void setSearchType(String searchType) {
        this.searchType = searchType;
    }
}

class Item {

    @JsonProperty("item_id")
    private String itemId;

    @JsonProperty("resolved_id")
    private String resolvedId;

    // more attributes here

    public String getItemId() {
        return itemId;
    }

    public void setItemId(String itemId) {
        this.itemId = itemId;
    }

    public String getResolvedId() {
        return resolvedId;
    }

    public void setResolvedId(String resolvedId) {
        this.resolvedId = resolvedId;
    }
}

Here is more information on parsing JSON.

You can't parse out that list object reasonably well since the numbers are random-ish. You'll need to make that a Map. Otherwise, the rest of the data is parsable by Gson.

class Foo {
    int status;
    int complete;
    TreeMap<String, Object> list;
    Object error;
    SearchMeta search_meta;
    long since;
}

class SearchMeta {
    String search_type;
}

You can replace the <String, Object> map with <String, InnerObject>, where InnerObject is a POJO for this object

{
    "item_id": "734233858",
    "resolved_id": "734233858",
    "given_url": "https://blog.openshift.com/developing-single-page-web-applications-using-java-8-spark-mongodb-and-angularjs/",
    "given_title": "",
    "favorite": "0",
    ...
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!