Android 改型无法解析JSON:BEGIN\u数组,但它是带有hackernews api的BEGIN\u对象
这是JSON fron 这是我的密码 使用www.jsonschema2pojo.org创建的文章POJOAndroid 改型无法解析JSON:BEGIN\u数组,但它是带有hackernews api的BEGIN\u对象,android,json,retrofit,Android,Json,Retrofit,这是JSON fron 这是我的密码 使用www.jsonschema2pojo.org创建的文章POJO @JsonInclude(JsonInclude.Include.NON_NULL) @JsonPropertyOrder({ "title", "url", "id", "commentCount", "points", "postedAgo", "postedBy" }) public class Article { @Json
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"title",
"url",
"id",
"commentCount",
"points",
"postedAgo",
"postedBy"
})
public class Article {
@JsonProperty("title")
private String title;
@JsonProperty("url")
private String url;
@JsonProperty("id")
private Integer id;
@JsonProperty("commentCount")
private Integer commentCount;
@JsonProperty("points")
private Integer points;
@JsonProperty("postedAgo")
private String postedAgo;
@JsonProperty("postedBy")
private String postedBy;
private String image;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
public String getImage() {return image;}
public void setImage(String image){this.image = image;}
@JsonProperty("title")
public String getTitle() {
return title;
}
@JsonProperty("title")
public void setTitle(String title) {
this.title = title;
}
public Article withTitle(String title) {
this.title = title;
return this;
}
@JsonProperty("url")
public String getUrl() {
return url;
}
@JsonProperty("url")
public void setUrl(String url) {
this.url = url;
}
public Article withUrl(String url) {
this.url = url;
return this;
}
@JsonProperty("id")
public Integer getId() {
return id;
}
@JsonProperty("id")
public void setId(Integer id) {
this.id = id;
}
public Article withId(Integer id) {
this.id = id;
return this;
}
@JsonProperty("commentCount")
public Integer getCommentCount() {
return commentCount;
}
@JsonProperty("commentCount")
public void setCommentCount(Integer commentCount) {
this.commentCount = commentCount;
}
public Article withCommentCount(Integer commentCount) {
this.commentCount = commentCount;
return this;
}
@JsonProperty("points")
public Integer getPoints() {
return points;
}
@JsonProperty("points")
public void setPoints(Integer points) {
this.points = points;
}
public Article withPoints(Integer points) {
this.points = points;
return this;
}
@JsonProperty("postedAgo")
public String getPostedAgo() {
return postedAgo;
}
@JsonProperty("postedAgo")
public void setPostedAgo(String postedAgo) {
this.postedAgo = postedAgo;
}
public Article withPostedAgo(String postedAgo) {
this.postedAgo = postedAgo;
return this;
}
@JsonProperty("postedBy")
public String getPostedBy() {
return postedBy;
}
@JsonProperty("postedBy")
public void setPostedBy(String postedBy) {
this.postedBy = postedBy;
}
public Article withPostedBy(String postedBy) {
this.postedBy = postedBy;
return this;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
@Override
public int hashCode() {
return HashCodeBuilder.reflectionHashCode(this);
}
@Override
public boolean equals(Object other) {
return EqualsBuilder.reflectionEquals(this, other);
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
public Article withAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
return this;
}
}
我用这个得到了json
ApiClient.getUnlockApiClient().getArticles(new Callback<List<Article>>(){
@Override
public void success(List<Article> articles, Response response) {
for (Article article : articles) {
Log.i(TAG,article.toJSON());
}
}
知道如何解决这个问题吗?您试图解析一个项目数组,但响应只是一个对象。因此,为了使其正常工作,您可以创建一个新类
Response
,该类将有两个字段nextId
和List
,为它们添加适当的注释,希望这能起到作用
之后,您可以从响应
对象中检索文章
的列表
}
private static UnlockApiInterface unlockService;
public static UnlockApiInterface getUnlockApiClient(){
if(unlockService==null){
RestAdapter restAdapter = new RestAdapter.Builder()
.setLogLevel(RestAdapter.LogLevel.FULL)
.setEndpoint("http://api.ihackernews.com")
.build();
unlockService = restAdapter.create(UnlockApiInterface.class);
}
return unlockService;
}
ApiClient.getUnlockApiClient().getArticles(new Callback<List<Article>>(){
@Override
public void success(List<Article> articles, Response response) {
for (Article article : articles) {
Log.i(TAG,article.toJSON());
}
}
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2