gpt4 book ai didi

java - 为什么 Gson 期望开始数组却得到一个对象呢?

转载 作者:行者123 更新时间:2023-11-29 05:00:01 25 4
gpt4 key购买 nike

我正在使用这个 JSON,它存储一些带有 Retrofit 的文章,但我有一个错误:

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

这是我的代码:

RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("http://www.example.com/")
.setLogLevel(RestAdapter.LogLevel.FULL)
.build();

Flow flowservice = restAdapter.create(Flow.class);

flowservice.getArticles("55ec320b066ee7ae08360f12", new Callback<List<String>>() {

@Override
public void success(List<String> objects, Response response) {
textView.setText(objects.get(0));
}

@Override
public void failure(RetrofitError error) {
textView.setText(error.getMessage());
}
});

我使用这个 POJO:

public class Article implements Serializable {
private int remoteId;
private String title;
private String imageUrl;
private String content;
private int viewsCount;
private int commentsCount;
private int likesCount;
private int categoryId;
private String authorName;
private boolean liked;

// getters and setters removed
}

和一个示例 JSON:

{  
"success":true,
"errorCode":0,
"articles":[
{
"remoteId":0,
"title":"Nam viverra vulputate lacus nec pellentesque. Nam viverra vulputate lacus nec pellentesque.",
"imageUrl":"/articles/armatis.png",
"content":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas interdum tempus ultrices. Ut quis tellus molestie, ornare mi non, fermentum nisl. Sed vitae ultricies metus. Vivamus aliquam metus enim, ut fringilla justo molestie et. Maecenas nec ligula neque. Mauris vel cursus risus. Suspendisse ut nulla porta, congue nulla sit amet.",
"viewsCount":235,
"commentsCount":6,
"likesCount":18,
"categoryId":1,
"authorName":"Sébastien Gabory",
"liked":false
},
{
"remoteId":1,
"title":"Nam viverra vulputate lacus nec pellentesque.",
"imageUrl":"/articles/armatis.png",
"content":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas interdum tempus ultrices. Ut quis tellus molestie, ornare mi non, fermentum nisl. Sed vitae ultricies metus. Vivamus aliquam metus enim, ut fringilla justo molestie et. Maecenas nec ligula neque. Mauris vel cursus risus. Suspendisse ut nulla porta, congue nulla sit amet.",
"viewsCount":18,
"commentsCount":6,
"likesCount":25,
"categoryId":1,
"authorName":"Sébastien Gabory",
"liked":false
}
]
}

流类:

public interface Flow {
@GET("/v2/{json}")
void getArticles (@Path("json") String json ,Callback<List<Article>> callback);
}

最佳答案

GSON 正在尝试将您的 JSON 响应直接转换为文章列表。但是,您的文章列表实际上嵌套在一个外部 JSON 对象的深处。当 GSON 尝试将此外部对象转换为 JSON 数组(以最终转换为 Java List)时,它会因此抛出您看到的错误:

Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2

解决此问题的一个方法是创建一个新的 POJO 来表示整个 JSON 响应。例如:

public class ArticleListWrapper implements Serializable {
private boolean success;
private double errorCode;
private List<Article> articles;

// getters and setters, etc.
}

然后您的 Flow 类将更新如下:

@GET("/v2/{json}")
void getArticleListWrapper (@Path("json") String json, Callback<ArticleListWrapper> callback);

GSON 应该能够正确地将 JSON 响应反序列化为 ArticleListWrapper 对象。您可以通过访问此 ArticleListWrapper 对象的 articles 字段来检索回调中的文章列表。

关于java - 为什么 Gson 期望开始数组却得到一个对象呢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32423906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com