1
我解析的JSON格式如下:使用Retrofit如何解析JSONArray而不解析對象?
{
msg: "success",
status_code: 200,
data: [
{
....
} ]
}
,我就在下面的對象使用Retorfit2解析:
public class Model {
@SerializedName("status_code")
int statusCode;
@SerializedName("msg")
private String statusMsg;
@SerializedName("data")
private JSONArray data;
}
我想「數據」分析,以JSONArray,而不是具體的型號類型,不管它的內部物體結構如何。但是,它給出了以下例外,這是什麼問題?
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
沒有,如果你想自己解析它,請使用ScalarConverterFactory接收字符串響應。 – TruongHieu
上面的代碼現在用JsonArray而不是JSONArray工作。 反正謝謝你的建議:)。 –