com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_ARRAY但STRING [英] com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING
问题描述
这是使用GSON系列化我的第一种方法。我recive Facebook的回应我喜欢这个Android应用程序
我的JSON:
{数据:
{
pic_square:HTTPS://fbcdn-profile-a.akamaihd.netxxxx1388091435_797626998_q.jpg
UID:10202xxx852765,
名:先生X
},
{
pic_square:HTTPS://fbcdn-profile-a.akamaihd.netxxxx1388091435_797626998_q.jpg
UID:10202xxx852765,
名:先生X
}
]
} 尝试{
最后GsonBuilder建设者=新GsonBuilder();
最后GSON GSON = builder.create();
JSONObject的数据= response.getGraphObject()getInnerJSONObject()。
FacebookResponses facebookResponses = gson.fromJson(data.toString(),FacebookResponses.class); //这里异常
Log.i(TAG,结果:+ facebookResponses.toString());
}赶上(JsonSyntaxException E){
e.printStackTrace();
}
我班
公共类FacebookResponses实现Serializable {
私有静态最后的serialVersionUID长1L =;
@SerializedName(数据);
私人FacebookRisp []的数据;
}类FacebookRisp实现Serializable { 私有静态最后的serialVersionUID长1L =; @SerializedName(pic_square)
私有String [] pic_square; @SerializedName(UID)
私有String [] UID; @SerializedName(名称)
私有String []名; 公众的String [] getPic_square(){
返回pic_square;
} 公共无效setPic_square(字符串[] pic_square){
this.pic_square = pic_square;
} 公众的String []的getuid(){
返回UID;
} 公共无效的setuid(字符串[] UID){
this.uid = UID;
} 公众的String []的getName(){
返回名称;
} 公共无效setname可以(字符串[]名称){
this.name =名称;
} }
我得到
com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_ARRAY但STRING在1号线列118
更新:
我修改的爱琴海答案,问题均 []
@SerializedName(pic_square)
私人字符串** [] ** pic_square; //这里恩等人
您 FacebookResponses
类更改这些:
私有类FacebookResponses {
私人数据[]的数据;
}私有类数据{
@SerializedName(pic_square)
私人字符串picSquare;
私人字符串的UID;
私人字符串名称;
}
编辑:因为你的JSON响应的结构如下图所示:
This is my first approach to serialization using Gson. I recive facebook response to my android application like this
My Json:
{"data": [
{
"pic_square": "https://fbcdn-profile-a.akamaihd.netxxxx1388091435_797626998_q.jpg",
"uid": "10202xxx852765",
"name": "Mister X"
},
{
"pic_square": "https://fbcdn-profile-a.akamaihd.netxxxx1388091435_797626998_q.jpg",
"uid": "10202xxx852765",
"name": "Mister X"
}
]
}
try {
final GsonBuilder builder = new GsonBuilder();
final Gson gson = builder.create();
JSONObject data= response.getGraphObject().getInnerJSONObject();
FacebookResponses facebookResponses= gson.fromJson(data.toString(),FacebookResponses.class); //exception here
Log.i(TAG, "Result: " + facebookResponses.toString());
} catch (JsonSyntaxException e) {
e.printStackTrace();
} My class
public class FacebookResponses implements Serializable {
private static final long serialVersionUID = 1L;
@SerializedName("data");
private FacebookRisp[] data;
}
class FacebookRisp implements Serializable {
private static final long serialVersionUID = 1L;
@SerializedName("pic_square")
private String[] pic_square;
@SerializedName("uid")
private String[] uid;
@SerializedName("name")
private String[] name;
public String[] getPic_square() {
return pic_square;
}
public void setPic_square(String[] pic_square) {
this.pic_square = pic_square;
}
public String[] getUid() {
return uid;
}
public void setUid(String[] uid) {
this.uid = uid;
}
public String[] getName() {
return name;
}
public void setName(String[] name) {
this.name = name;
}
}
I get com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING at line 1 column 118
UPDATE: I modified the answer of aegean, the problem were []
@SerializedName("pic_square")
private String**[]** pic_square; //ex here and others
Change your FacebookResponses
class to these:
private class FacebookResponses {
private Data[] data;
}
private class Data {
@SerializedName("pic_square")
private String picSquare;
private String uid;
private String name;
}
Edit: Because your json response's structure is like below:
这篇关于com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_ARRAY但STRING的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!