The automated JSON to POJO fails badly with this 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.
{
"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?
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",
...
来源:https://stackoverflow.com/questions/47188934/how-to-turn-this-json-response-into-pojo