英文:
Error parse json for retrofit 2, expected begin array but was begin object
问题
{
"Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path": "预期是BEGIN_ARRAY,但在第1行第2列路径处是BEGIN_OBJECT",
"My interface looks like this": "我的接口如下",
"public interface UsersApi {": "public interface UsersApi {",
"@GET("/api/?results=20")": "@GET("/api/?results=20")",
"Call<List<Result>> getData();": "Call<List
"Json API": "Json API",
"{": "{",
""results": [": ""results": [",
"{": "{",
""gender":"female"," : ""gender":"female",",
""name":{" : ""name":{",
""title":"Miss"," : ""title":"Miss",",
""first":"Lana"," : ""first":"Lana",",
""last":"Da Silva"" : ""last":"Da Silva""
}
英文:
Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path
My interface looks like this
public interface UsersApi {
@GET("/api/?results=20")
Call<List<Result>> getData();
}
Json API
{
"results": [
{
"gender":"female",
"name":{
"title":"Miss",
"first":"Lana",
"last":"Da Silva"
}
}
]
}
答案1
得分: 2
请创建以下示例中的模型类,例如:
public class Modal {
@SerializedName("results")
private List<Result> mResults;
public List<Result> getResults() {
return mResults;
}
public void setResults(List<Result> results) {
mResults = results;
}
}
创建名为 "UsersApi" 的接口:
public interface UsersApi {
@GET("/api/?results=20")
Call<Modal> getData();
}
英文:
Please Create modal Class like below e.g.
public class Modal {
@SerializedName("results")
private List<Result> mResults;
public List<Result> getResults() {
return mResults;
}
public void setResults(List<Result> results) {
mResults = results;
}
}
public interface UsersApi {
@GET("/api/?results=20")
Call<Modal> getData();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论