错误解析 JSON 用于 Retrofit 2,预期是开始数组但实际是开始对象。

huangapple go评论78阅读模式
英文:

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> getData();",
"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(&quot;/api/?results=20&quot;)
   Call&lt;List&lt;Result&gt;&gt; getData();
}

Json API

{
   &quot;results&quot;: [
      {
         &quot;gender&quot;:&quot;female&quot;,
         &quot;name&quot;:{
            &quot;title&quot;:&quot;Miss&quot;,
            &quot;first&quot;:&quot;Lana&quot;,
            &quot;last&quot;:&quot;Da Silva&quot;
            }
      }
    ]
}

答案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(&quot;results&quot;)
private List&lt;Result&gt; mResults;

public List&lt;Result&gt; getResults() {
    return mResults;
}

public void setResults(List&lt;Result&gt; results) {
    mResults = results;
}

}



public interface UsersApi {
@GET(&quot;/api/?results=20&quot;)
Call&lt;Modal&gt; getData();
}

huangapple
  • 本文由 发表于 2020年7月30日 18:59:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/63171737.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定