当JSON只有对象时创建POJO列表

时间:2017-10-30 07:49:13

标签: java json retrofit retrofit2 pojo

我正在使用Pocket API获取已添加书签的文章及其网址列表,有数百种,这里只有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
}

正如您所看到的,"list": {}有很多项,但它是一个数组,它包含对象。所以当我尝试使用http://www.jsonschema2pojo.org生成POJO时,我得到以每个项目的ID命名的POJOS:

package model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class _1015284226 {

    @SerializedName("item_id")
    @Expose
    private String itemId;
    @SerializedName("resolved_id")
    @Expose
    private String resolvedId;
    @SerializedName("given_url")
    @Expose
    private String givenUrl;
    @SerializedName("given_title")
    @Expose
    private String givenTitle;
    @SerializedName("favorite")
    @Expose
    private String favorite;
    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("time_added")
    @Expose
    private String timeAdded;
    @SerializedName("time_updated")
    @Expose
    private String timeUpdated;
    @SerializedName("time_read")
    @Expose
    private String timeRead;
    @SerializedName("time_favorited")
    @Expose
    private String timeFavorited;
    @SerializedName("sort_id")
    @Expose
    private Integer sortId;
    @SerializedName("resolved_title")
    @Expose
    private String resolvedTitle;
    @SerializedName("resolved_url")
    @Expose
    private String resolvedUrl;
    @SerializedName("excerpt")
    @Expose
    private String excerpt;
    @SerializedName("is_article")
    @Expose
    private String isArticle;
    @SerializedName("is_index")
    @Expose
    private String isIndex;
    @SerializedName("has_video")
    @Expose
    private String hasVideo;
    @SerializedName("has_image")
    @Expose
    private String hasImage;
    @SerializedName("word_count")
    @Expose
    private String wordCount;

    /**
     * No args constructor for use in serialization
     * 
     */
    public _1015284226() {
    }

    /**
     * 
     * @param hasImage
     * @param givenUrl
     * @param status
     * @param timeFavorited
     * @param isIndex
     * @param excerpt
     * @param resolvedId
     * @param sortId
     * @param givenTitle
     * @param timeUpdated
     * @param isArticle
     * @param wordCount
     * @param itemId
     * @param favorite
     * @param timeAdded
     * @param hasVideo
     * @param resolvedUrl
     * @param resolvedTitle
     * @param timeRead
     */
    public _1015284226(String itemId, String resolvedId, String givenUrl, String givenTitle, String favorite, String status, String timeAdded, String timeUpdated, String timeRead, String timeFavorited, Integer sortId, String resolvedTitle, String resolvedUrl, String excerpt, String isArticle, String isIndex, String hasVideo, String hasImage, String wordCount) {
        super();
        this.itemId = itemId;
        this.resolvedId = resolvedId;
        this.givenUrl = givenUrl;
        this.givenTitle = givenTitle;
        this.favorite = favorite;
        this.status = status;
        this.timeAdded = timeAdded;
        this.timeUpdated = timeUpdated;
        this.timeRead = timeRead;
        this.timeFavorited = timeFavorited;
        this.sortId = sortId;
        this.resolvedTitle = resolvedTitle;
        this.resolvedUrl = resolvedUrl;
        this.excerpt = excerpt;
        this.isArticle = isArticle;
        this.isIndex = isIndex;
        this.hasVideo = hasVideo;
        this.hasImage = hasImage;
        this.wordCount = wordCount;
    }

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

    public String getGivenUrl() {
        return givenUrl;
    }

    public void setGivenUrl(String givenUrl) {
        this.givenUrl = givenUrl;
    }

    public String getGivenTitle() {
        return givenTitle;
    }

    public void setGivenTitle(String givenTitle) {
        this.givenTitle = givenTitle;
    }

    public String getFavorite() {
        return favorite;
    }

    public void setFavorite(String favorite) {
        this.favorite = favorite;
    }

    public String getStatus() {
        return status;
    }

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

    public String getTimeAdded() {
        return timeAdded;
    }

    public void setTimeAdded(String timeAdded) {
        this.timeAdded = timeAdded;
    }

    public String getTimeUpdated() {
        return timeUpdated;
    }

    public void setTimeUpdated(String timeUpdated) {
        this.timeUpdated = timeUpdated;
    }

    public String getTimeRead() {
        return timeRead;
    }

    public void setTimeRead(String timeRead) {
        this.timeRead = timeRead;
    }

    public String getTimeFavorited() {
        return timeFavorited;
    }

    public void setTimeFavorited(String timeFavorited) {
        this.timeFavorited = timeFavorited;
    }

    public Integer getSortId() {
        return sortId;
    }

    public void setSortId(Integer sortId) {
        this.sortId = sortId;
    }

    public String getResolvedTitle() {
        return resolvedTitle;
    }

    public void setResolvedTitle(String resolvedTitle) {
        this.resolvedTitle = resolvedTitle;
    }

    public String getResolvedUrl() {
        return resolvedUrl;
    }

    public void setResolvedUrl(String resolvedUrl) {
        this.resolvedUrl = resolvedUrl;
    }

    public String getExcerpt() {
        return excerpt;
    }

    public void setExcerpt(String excerpt) {
        this.excerpt = excerpt;
    }

    public String getIsArticle() {
        return isArticle;
    }

    public void setIsArticle(String isArticle) {
        this.isArticle = isArticle;
    }

    public String getIsIndex() {
        return isIndex;
    }

    public void setIsIndex(String isIndex) {
        this.isIndex = isIndex;
    }

    public String getHasVideo() {
        return hasVideo;
    }

    public void setHasVideo(String hasVideo) {
        this.hasVideo = hasVideo;
    }

    public String getHasImage() {
        return hasImage;
    }

    public void setHasImage(String hasImage) {
        this.hasImage = hasImage;
    }

    public String getWordCount() {
        return wordCount;
    }

    public void setWordCount(String wordCount) {
        this.wordCount = wordCount;
    }

}

包含项目的List POJO创建为:

package model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class List {

    @SerializedName("1015284226")
    @Expose
    private model._1015284226 _1015284226;

    /**
     * No args constructor for use in serialization
     * 
     */
    public List() {
    }

    /**
     * 
     * @param _1015284226
     */
    public List(model._1015284226 _1015284226) {
        super();
        this._1015284226 = _1015284226;
    }

    public model._1015284226 get1015284226() {
        return _1015284226;
    }

    public void set1015284226(model._1015284226 _1015284226) {
        this._1015284226 = _1015284226;
    }

}

当我尝试解析JSON时,这显然给了我一些问题

我正在使用改装2。

我在想我应该重构List POJO,因此它包含一个项目的ArrayList,但是不想过多地为我自动生成的内容做错。

1 个答案:

答案 0 :(得分:0)

你可以像这样手动完成。

        JSONObject jsonObj = new JSONObject(json);

        JSONObject lists= jsonObj.getJSONObject("list");
        Iterator x = lists.keys();
        JSONArray jsonArray = new JSONArray();

        while (x.hasNext()){
            String key = (String) x.next();
            jsonArray.put(lists.get(key));
        }

jsonArray是你的列表

之后,您可以将JSON解析为object。