简体   繁体   English

如何将此JSON响应转换为POJO?

[英]How to turn this JSON response into POJO?

The automated JSON to POJO fails badly with this JSON. 此JSON到POJO的自动JSON严重失败。

Please note that the number of items is different from one request to the other. 请注意,一个请求与另一个请求的项目数不同。 here I'm including JSON response with 2 items. 在这里,我将包括2个项目的JSON响应。

{
    "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
}

How would the POJOs for this JSON object would look like? 这个JSON对象的POJO看起来如何?

You can't parse out that list object reasonably well since the numbers are random-ish. 您不能很好地解析该list对象,因为数字是随机的。 You'll need to make that a Map. 您需要将其制作为地图。 Otherwise, the rest of the data is parsable by Gson. 否则,其余数据可由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 您可以将<String, Object>映射替换为<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",
    ...

Ideally, the list property in the JSON object should actually be an array of elements instead of inner JSON objects. 理想情况下,JSON对象中的list属性实际上应该是元素数组,而不是内部JSON对象。 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. 是有关解析JSON的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM