使用此JSON,自动JSON到POJO严重失败。
请注意,不同请求的项目数量不同。在这里,我将包含两项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<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;
}
}
下面是有关解析JSON的更多信息。
你不能很好地解析出list
对象,因为这些数字是随机的。你需要把它做成一张地图。否则,其余数据可由Gson解析。
class Foo {
int status;
int complete;
TreeMap<String, Object> list;
Object error;
SearchMeta search_meta;
long since;
}
class SearchMeta {
String search_type;
}
您可以替换
{
"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",
...
我需要将其转换为以下格式: 类型的数量可以改变(例如,可以只有A和B)。有人能帮我吗?我使用这个组件在网站https://js.devexpress.com/demos/widgetsgallery/demo/datagrid/simplearray/angular/light/上显示数据
我是格森的新手。我需要将下面的JSON响应转换成一个列表。 JSON响应: 我有一门课要教数据 账户JAVA 当我对我的班级做出回应时,我得到了: 现在我需要将这两个值放入
这是我的JSON回复 但这就是未来的样子 正如您在第一个代码片段中看到的,“状态”为1表示没有异常,而在第二个代码片段中,“状态”为2表示更改,因此数组已填充。 我如何解析这两个??
我有一个问题,我已经在stackoverflow上问了几次,我已经尝试了所有这些问题,没有一个有效。所以我很想在另一个时间总结这个问题,并试着更准确地描述它。 我正在构建一个应用程序,将图片发送到python后端,以获得xcode swift中的图像识别结果。 我正在使用Alamofire上传,这里是上传部分: 下面是我从服务器端得到的json响应: 所以,我想要达到的目的,就是打印第一个预言的名
我可以使用现在已弃用的方法将WebClient响应转换为响应实体。 请建议实现相同结果的其他方法。下面是我的代码。
问题内容: 我无法将SOAP响应转换为php中的Array。 这是代码 这是SOAP响应。 我使用以下方法将其转换为数组,但输出为空。 请给我建议。 问题答案: 终于我找到了解决方案 我们可以通过以下方式从SOAP获得响应主体 范例1: 示例2: