当前位置: 首页 > 知识库问答 >
问题:

reverfit-java.lang.IllegalStateException:应为BEGIN_ARRAY,但为BEGIN_OBJECT

胡沈义
2023-03-14
{
    "type":"success",
    "value":[
        {
            "id":1,
            "title":"Title - 1",
         "name":{
            "first":"First - 1",
            "last":"Last - 1"
         },
            "hobbies":[
                "Writing Code - 1",
            "Listening Music - 1"
            ]
        },
       .....
    ]
}
E/app.retrofit_chucknorries.MainActivity$2: ERROR: com.google.gson.JsonSyntaxException: 
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT 
at line 7 column 12 path $.value[0].name
01-21 12:41:52.156 28936-28936/app.retrofit_chucknorries 
W/System.err: retrofit.RetrofitError: com.google.gson.JsonSyntaxException: 
java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT 
at line 7 column 12 path $.value[0].name
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.ArrayList;
import java.util.List;

public class Value {

    @SerializedName("id")
    @Expose
    private Integer id;

    @SerializedName("title")
    @Expose
    private String title;

    @SerializedName("hobbies")
    @Expose
    private List<String> hobbies = new ArrayList<String>();

    @SerializedName("name")
    @Expose
    private List<Name> name = new ArrayList<Name>();

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public List<Name> getName() {
        return name;
    }

    public void setName(List<Name> name) {
        this.name = name;
    }

    public List<String> getHobbies() {
        return hobbies;
    }

    public void setHobbies(List<String> hobbies) {
        this.hobbies = hobbies;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

共有1个答案

葛雪松
2023-03-14

在您的master.java类中,您的名称不是一个数组!

private List<Name> name = new ArrayList<Name>();

改为此,然后尝试:

 private Name name;

实际上,通过查看异常的日志,您可以看出这一点。

 类似资料: