Je suis analyse syntaxique format suivant de JSON:Utilisation de Retrofit comment analyser JSONArray sans analyse d'objets?
{
msg: "success",
status_code: 200,
data: [
{
....
} ]
}
et je suis l'analyse à l'aide Retorfit2 dans l'objet suivant:
public class Model {
@SerializedName("status_code")
int statusCode;
@SerializedName("msg")
private String statusMsg;
@SerializedName("data")
private JSONArray data;
}
Je veux analyser « données » à JSONArray au lieu de types de modèles spécifiques, indépendamment de sa structure d'objets internes. Mais il donne suite à l'exception, quel est le problème?
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
Non, Si vous voulez analyser par vous-même, s'il vous plaît utiliser ScalarConverterFactory pour recevoir une réponse String. – TruongHieu
ci-dessus code travaille maintenant pour moi avec JsonArray au lieu de JSONArray. En tout cas merci pour votre suggestion :). –