[英]com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING
This is my first approach to serialization using Gson. 这是我使用Gson进行序列化的第一种方法。 I recive facebook response to my android application like this
我像这样回复了我的Android应用程序的facebook响应
My Json: 我的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 我收到了com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_ARRAY但在第1行第118列为STRING
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: 将您的
FacebookResponses
类更改为:
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: 编辑:因为您的json响应的结构如下所示:
MalformedJsonException Thrown when a reader encounters malformed JSON.
MalformedJsonException当读者遇到格式错误的JSON时抛出。 Some syntax errors can be ignored by calling setLenient(boolean).
调用setLenient(boolean)可以忽略一些语法错误。
Its difficult to find solution but seems like that your JSON response is not valid. 很难找到解决方案,但似乎您的JSON响应无效。 Check here
点击这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.