com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_ARRAY但是STRING

时间:2013-12-27 12:49:33

标签: java android serialization facebook-fql gson

这是我使用Gson进行序列化的第一种方法。我在这个

中找到了对我的Android应用程序的Facebook响应

我的杰森:

 {"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();

} 我的班级

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;
   }

 }

我明白了 com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期为BEGIN_ARRAY但在第1行第118列为STRING

更新: 我修改了爱琴海的答案,问题是 []

@SerializedName("pic_square")
private String**[]** pic_square;   //ex here and others

2 个答案:

答案 0 :(得分:4)

将您的FacebookResponses课程更改为:

private class FacebookResponses {
    private Data[] data;
}

private class Data {
    @SerializedName("pic_square")
    private String picSquare;
    private String uid;
    private String name;
}

编辑:因为您的json响应的结构如下所示:

enter image description here

答案 1 :(得分:1)

  

MalformedJsonException当读者遇到格式错误的JSON时抛出。调用setLenient(boolean)可以忽略一些语法错误。

很难找到解决方案,但似乎您的JSON响应无效。 Check here