如何将这个 JSON 响应变成 POJO?

How to turn this JSON response into POJO?

POJO 的自动化 JSON 严重失败 JSON。

请注意,一个请求与另一个请求的商品数量不同。 在这里,我将 JSON 回复包含 2 个项目。

{
    "status": 1,
    "complete": 1,
    "list": {
        "734233858": {
            "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",
            "status": "0",
            "time_added": "1466459879",
            "time_updated": "1466459862",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 1,
            "resolved_title": "Developing Single Page Web Applications using Java 8, Spark, MongoDB, and AngularJS",
            "resolved_url": "https://blog.openshift.com/developing-single-page-web-applications-using-java-8-spark-mongodb-and-angularjs/",
            "excerpt": "In this post you will learn how to use a micro framework called Spark to build a RESTful backend. The RESTful backend is consumed by a single page web application using AngularJS and MongoDB for data storage. I’ll also show you how to run Java 8 on OpenShift.",
            "is_article": "1",
            "is_index": "0",
            "has_video": "0",
            "has_image": "1",
            "word_count": "2727"
        },
        "1015284226": {
            "item_id": "1015284226",
            "resolved_id": "1015284226",
            "given_url": "https://sparktutorials.github.io/2015/08/04/spark-video-tutorials.html",
            "given_title": "",
            "favorite": "0",
            "status": "0",
            "time_added": "1466458750",
            "time_updated": "1466458737",
            "time_read": "0",
            "time_favorited": "0",
            "sort_id": 0,
            "resolved_title": "Spark Video Tutorials",
            "resolved_url": "http://sparktutorials.github.io/2015/08/04/spark-video-tutorials.html",
            "excerpt": "Our friends over at learnhowtoprogram.com have been working on a series of Java courses for beginners, all of which feature Spark. This post contains an overview of these courses with direct links to their videos.",
            "is_article": "1",
            "is_index": "0",
            "has_video": "0",
            "has_image": "0",
            "word_count": "41"
        }
    },
    "error": null,
    "search_meta": {
        "search_type": "normal"
    },
    "since": 1509309762
}

这个 JSON 对象的 POJO 会是什么样子?

您无法很好地解析出 list 对象,因为这些数字是随机的。您需要将其设为地图。否则,其余数据可由 Gson 解析。

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

class SearchMeta {
    String search_type;
}

您可以将 <String, Object> 映射替换为 <String, InnerObject>,其中 InnerObject 是此对象的 POJO

{
    "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",
    ...

理想情况下,JSON 对象中的 list 属性 实际上应该是一个元素数组,而不是内部 JSON 对象。但是,您可以使用以下方法对数据建模。

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 是关于解析 JSON.

的更多信息