Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 319
alfo888_ibg Avatar asked Nov 30 '25 19:11

alfo888_ibg


1 Answers

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

like image 179
Devrim Avatar answered Dec 02 '25 07:12

Devrim