- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在使用 Pocket API 获取已添加书签的文章及其 URL 的列表,有数百篇文章,下面是一个只有 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;
}
}
包含项目的列表 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,但不想过多地处理自动生成的内容。
最佳答案
您可以像这样手动完成。
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 解析为对象。
关于java - 当 JSON 只有对象时创建 POJO 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47010253/
我刚刚发现了 Camel,它似乎正是我所需要的。 我有几个知道如何处理特定输入数据的构建 block ,所以我想创建一个非常简单的 GUI 供用户选择构建 block ,只需将它们一个接一个地链接起来
根据Convert Spark DataFrame to Pojo Object的回复我了解到 Dataframe是 Dataset 的别名. 我目前计算了 JavaPairRDD哪里CityStat
我使用的是jackson,场景是: 在客户端 (Android) 中生成对 Web 服务的 URL 调用 Web 服务以 json 形式返回“complex pojo”(具有与其他 pojo 相同的类
我需要将文档存储到 Elasticsearch 索引中,因此我定义了一个映射。我需要从我的 java 客户端向它提供看起来像 Compony 类的 pojo。它有很多重复的值。我可以使用对象组合模式来
所以我正在开发一个聊天应用程序。有一部分我感兴趣的是一次从数据库中检索 3 个实体。有一个 serverDto 实体和 accountDto 实体。每个服务器都有很多账户,账户里有一个字段叫做acti
我有一个 Java 中的 POJO 列表,我需要将其转换为另一种类型的 POJO 列表。两个 POJO 都有两个字符串字段。除了迭代原始列表并将新项目添加到结果列表之外,是否有更好的方法来执行此操作?
我正在开发一个 API 来访问存储在系统中的数据。该系统包含人员、约会和与这些约会相关的程序等内容。我的应用程序将严格为只读。 我正在使用带 RowMapper 的 Spring 来构建对象,例如“P
希望有人能帮助我。我有一个具有以下结构的 POJO: public class Invoice{ private String docNum; private String customer; pri
引用以下链接,我在运行时创建了类,http://blog.javaforge.net/post/31913732423/howto-create-java-pojo-at-runtime-with-j
所以我遇到了这个教程,将 POJO 序列化为 json,然后将 json 文件反序列化回 POJO。 http://www.mkyong.com/java/how-to-convert-java-ob
我是 Java 的新手。我只是对序列化和反序列化感到困惑。所以,我很困惑我应该使用哪一个。 我正在寻找一个回合,发现 Boon、Jackson、GSON(我目前正在使用 GSON,但一些文章使用 Ja
我确信这个问题可能已经被问过几次了,但我不明白我应该使用什么查询。我想要做的是,将 POJO 传递给另一个参数可以是动态的 POJO 像下面的例子 Class DataPOJO{ privat
我有一个有多页的注册表。所以,我将一页映射到一个 pojo。最后,当我处理数据库时,我想将其作为单个 pojo 提交。任何简化此模型的想法。 谢谢:) 最佳答案 您可以创建一个包装器 POJO,它在主
希望有人能帮助我。我有一个具有以下结构的 POJO: public class Invoice{ private String docNum; private String customer; pri
要求: 将 json 文件中选定的字段收集到 POJO 中(字段名称不匹配) 更新 POJO 的其他非映射字段 转换回带有 POJO 字段名的 jsonNode 问题:步骤 1 和 2 没问题。当将
我的 Spring Boot 应用程序中有两个 Pojo Pojo 1:FeedData 代码(我有获取和设置,只是没有在这里显示): package com.cms.tb.model; import
我们有两个 Pojo 文件。 Person { String name; int age; String address; String phoneNo; boolean isMa
我有一个数据库调用,如果没有任何条件匹配,则可能返回 null。如果有记录匹配,则结果是一个包含嵌入对象列表的 Pojo。我想将该 Pojo 转换为其嵌入对象 id 的列表。 Foo.class 有
我正在尝试使用流将 pojo 映射到 pojo 上的属性公开的一组项目。我意识到这还不清楚,所以我将展示我是如何在没有流的情况下完成的。 我有产品类别的枚举,产品的枚举和具有此类产品的商店列表 pub
我们在 Apache2 网络服务器后面的 Tomcat 6 中运行的 AXIS2 v1.5.2 中实现了一个 POJO 网络服务。 出于演示目的,我将重点介绍 validateUser 消息和相应的方
我是一名优秀的程序员,十分优秀!