-1

I/m trying to put json to recyclerview using GSON but when i run it it gives me "Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $" error!

here is my json :

{"action":"true","error":"","data":[{"_id":"58ad8d8ca49d0e11e21c4504","store_name":"firstStore","store_view":0,"store_textposition":null},{"_id":"58ad9063cb35f5977b55dfd6","store_name":"firstStorestest","store_view":0,"store_textposition":null},{"_id":"58ad9088cb35f5977b55dfd7","store_name":"firstStorestest","store_view":0,"store_textposition":null}]}

and here is where i'm getting the error :

    private void requestJsonObject(){
    RequestQueue queue = Volley.newRequestQueue(getContext());
    String url ="MYURL";
    StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Response " + response);
            GsonBuilder builder = new GsonBuilder();
            Gson mGson = builder.create();
            List<ItemObject> posts = new ArrayList<ItemObject>();
            posts = Arrays.asList(mGson.fromJson(response, ItemObject[].class));
            adapter = new RecyclerViewAdapter(getActivity(), posts);
            recyclerView.setAdapter(adapter);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(TAG, "Error " + error.getMessage());
        }
    });
    queue.add(stringRequest);
}

and here is Itemobject.java

public class ItemObject {
@SerializedName("store_name")
private String store_name;
@SerializedName("store_textposition")
private String store_textposition;
@SerializedName("store_view")
private String store_view;
@SerializedName("_id")
private String _id;
public ItemObject(String store_name, String store_textposition, String store_view, String _id) {
    this.store_name = store_name;
    this.store_textposition = store_textposition;
    this.store_view = store_view;
    this._id = _id;
}
public String getStore_name() {
    return store_name;
}
public String getStore_textposition() {
    return store_textposition;
}
public String getStore_view() {
    return store_view;
}
public String get_id(){
    return _id;
}

What's wrong with it?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
akdhb uhi
  • 39
  • 1
  • 6

1 Answers1

0

Your response starts with Object but You used to convert in Array.

EDIT

Create one class.

Class Test
{
    @SerializedName("data")
    private List<ItemObject> data;
}

Then write this code when parsing:

        Gson mGson = builder.create();
        Test test=mGson.fromJson(response, Test.class)
Asif Patel
  • 1,744
  • 1
  • 20
  • 27
  • could you please tell me what to do? or what to change @asif-patel – akdhb uhi Mar 03 '17 at 18:30
  • @akdhbuhi `mGson.fromJson(response, ItemObject[].class)`... Your data is **not** a `ItemObject[]` because it starts with `{` character, and not a `[` character. You need to extract the `"data"` value from the JSON before using Gson – OneCricketeer Mar 03 '17 at 18:38