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

使用此JSON,自动JSON到POJO失败了。

请注意,项目数量因一个请求而异。 在这里,我包括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 } 

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

理想情况下,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 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 getList() { return list; } public void setList(Map 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; } } 

以下是有关解析JSON的更多信息。

由于数字是随机的,因此您无法合理地解析该list对象。 你需要制作一张地图。 否则,其余数据可由Gson解析。

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

您可以使用替换映射,其中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", ...