【问题标题】:Retrofit cannot parse JSON : BEGIN_ARRAY but was BEGIN_OBJECT with hackernews apiRetrofit 无法解析 JSON:BEGIN_ARRAY 但使用hackernews api 是 BEGIN_OBJECT
【发布时间】:2014-09-01 19:57:25
【问题描述】:

这是来自http://api.ihackernews.com/page的JSON

{
    nextId: "2",
    items: [
        {
            title: "How I Start: Go",
            url: "http://howistart.org/posts/go/1",
            id: 8254143,
            commentCount: 2,
            points: 41,
            postedAgo: "57 minutes ago",
            postedBy: "dokuda"
        },
        {
            title: "Plants in offices increase happiness and productivity",
            url: "http://www.theguardian.com/money/2014/aug/31/plants-offices-workers-productive-                         minimalist-employees",
            id: 8253979,
            commentCount: 20,
            points: 60,
            postedAgo: "1 hour ago",
            postedBy: "dsr12"
        }
    ]
}    

这是我的代码

使用 www.jsonschema2pojo.org 创建的文章 POJO

@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;
}

}

ApiClient 公共类 ApiClient { 公共接口 UnlockApiInterface{ @GET("/page") void getArticles(Callback> 回调);

}

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;
}

我用这个得到了 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());
            }

        }

但我得到了这个错误

com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

有什么办法可以解决这个问题吗?

【问题讨论】:

    标签: android json retrofit


    【解决方案1】:

    您正在尝试解析一组项目,但响应只是一个对象。所以为了让它工作,你可以创建一个新类Response,它有两个字段nextId和List&lt;Article&gt;,为它们添加适当的注释,希望这能解决问题。

    之后,您可以从Response 对象中检索Article 的列表。

    【讨论】:

      猜你喜欢
      • 2021-01-07
      • 2020-06-07
      • 2015-11-10
      • 2019-02-24
      • 1970-01-01
      • 2018-01-12
      • 2019-06-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多