alfo888_ibg
alfo888_ibg

Reputation: 1857

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

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

Upvotes: 3

Views: 8310

Answers (2)

Devrim
Devrim

Reputation: 15533

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:

enter image description here

Upvotes: 4

Chintan Khetiya
Chintan Khetiya

Reputation: 16152

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

Upvotes: 1

Related Questions