com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was STRING

喜你入骨 提交于 2019-12-23 18:30:14

问题


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

回答1:


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:




回答2:


MalformedJsonException Thrown when a reader encounters malformed JSON. Some syntax errors can be ignored by calling setLenient(boolean).

Its difficult to find solution but seems like that your JSON response is not valid. Check here



来源:https://stackoverflow.com/questions/20801029/com-google-gson-jsonsyntaxexception-java-lang-illegalstateexception-expected-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!