本文介绍了改进 - java.lang.IllegalStateException:期望BEGIN_ARRAY但是BEGIN_OBJECT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图解析自己的JSON,但得到 JSONSyntaxException
,这里是我的 JSON 的样子:
I am trying to parse my own JSON, but getting JSONSyntaxException
, here is how my JSON looks:
{
"type":"success",
"value":[
{
"id":1,
"title":"Title - 1",
"name":{
"first":"First - 1",
"last":"Last - 1"
},
"hobbies":[
"Writing Code - 1",
"Listening Music - 1"
]
},
.....
]
}
Log说:
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
我在做什么错误?
我只是按照我的 class
else else与原始代码几乎相同
Value.java:
Where I am doing mistake ?
I just made few small modifications as per my requirement and classes
else everything almost same as in original codeValue.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;
}
}
推荐答案
你的Master.java类,你的名字不是数组!
In your Master.java class, your name is not an array!
private List<Name> name = new ArrayList<Name>();
更改为此,请尝试:
Change to this instead and try:
private Name name;
实际上,通过查看异常的日志,您可以知道这一点。
Actually by seeing the logs of the exception you can tell this.
这篇关于改进 - java.lang.IllegalStateException:期望BEGIN_ARRAY但是BEGIN_OBJECT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!