1

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
2
  • No, If you want to parse it by yourself, please use ScalarConverterFactory to receive String response. Commented Feb 13, 2017 at 10:34
  • above code is now working for me with JsonArray instead of JSONArray . Anyways thanks for your suggestion :) . Commented Feb 13, 2017 at 10:40

1 Answer 1

1

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;

}

`

Sign up to request clarification or add additional context in comments.

1 Comment

converting JSONArray to JsonArray solved my problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.