Usman Rana
Usman Rana

Reputation: 2142

Using Retrofit how to parse JSONArray without objects parsing?

I am parsing following format of Json:

{
msg: "success",
status_code: 200,
data: [
{
....
}  ]
}

and i am parsing it using Retorfit2 in following object:

public class Model {
    @SerializedName("status_code")
    int statusCode;
    @SerializedName("msg")
    private String statusMsg;
    @SerializedName("data")
    private JSONArray data;

}

I want to parse 'data' to JSONArray instead of specific Model types,regardless of its inner objects structure. But it gives following exception,what is the issue?

  W/System.err: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 44 path $.data
W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:224)
W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:129)
W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
W/System.err:     at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:37)
W/System.err:     at retrofit2.converter.gson.GsonResponseBodyConverter.convert(GsonResponseBodyConverter.java:25)
W/System.err:     at retrofit2.ServiceMethod.toResponse(ServiceMethod.java:117)
W/System.err:     at retrofit2.OkHttpCall.parseResponse(OkHttpCall.java:211)
W/System.err:     at retrofit2.OkHttpCall$1.onResponse(OkHttpCall.java:106)
W/System.err:     at okhttp3.RealCall$AsyncCall.execute(RealCall.java:133)
W/System.err:     at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
W/System.err:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
W/System.err:     at java.lang.Thread.run(Thread.java:761)
W/System.err: Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 44 path $.data
W/System.err:     at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:385)
W/System.err:     at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:213)
W/System.err:   ... 12 more

Upvotes: 1

Views: 818

Answers (1)

Tarun Dholakiya
Tarun Dholakiya

Reputation: 839

Take a look on below line of code.

`

public class Model {
    @SerializedName("status_code")
    int statusCode;
    @SerializedName("msg")
    private String statusMsg;
    @SerializedName("data")
    private **ArrayList<JSONObject>** data;

}

`

Upvotes: 1

Related Questions