编程语言
首页 > 编程语言> > com.google.gson.JsonSyntaxException:java.lang.IllegalStateException:预期BEGIN_ARRAY但是STRING

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

作者:互联网

这是我使用Gson进行序列化的第一种方法.我像这样回复了我的Android应用程序的facebook响应

我的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();

}
我的课

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

解决方法:

将您的FacebookResponses类更改为:

private class FacebookResponses {
    private Data[] data;
}

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

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

标签:android,java,gson,serialization,facebook-fql
来源: https://codeday.me/bug/20190831/1773945.html