Retrofit - java.lang.IllegalStateException:预期BEGIN_ARRAY但是BEGIN_OBJECT

时间:2016-01-21 07:26:55

标签: java android json gson retrofit

我正在尝试解析自己的JSON,但获得JSONSyntaxException,这就是我的 JSON 的外观:

{
    "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

我正在做的事mistake ?我根据我的requirement and classes进行了一些小修改,否则一切都与原始代码几乎相同 的 Value.java:

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 个答案:

答案 0 :(得分:4)

在您的Master.java类中,您的名字不是数组!

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

转而更改为此并尝试:

 private Name name;

实际上,通过查看异常日志,您可以告诉它。