使用GSON JSON包围Android
作者:互联网
尝试使用下面的2行使用GSON库读取和存储json信封时,应用程序崩溃(请参阅错误日志).解析有什么问题.
Type listType = new TypeToken<ArrayList<MRechercheResult>>() {}.getType();
List<MRechercheResult>search_results = new Gson().fromJson(envelop, listType);
错误日志
04-21 11:16:59.971: E/AndroidRuntime(19633): FATAL EXCEPTION: main
04-21 11:16:59.971: E/AndroidRuntime(19633): com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 178
04-21 11:16:59.971: E/AndroidRuntime(19633): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:176)
04-21 11:16:59.971: E/AndroidRuntime(19633): at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
04-21 11:16:59.971: E/AndroidRuntime(19633): at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:81)
04-21 11:16:59.971: E/AndroidRuntime(19633): at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:60)
04-21 11:16:59.971: E/AndroidRuntime(19633): at com.google.gson.Gson.fromJson(Gson.java:803)
04-21 11:16:59.971: E/AndroidRuntime(19633): at com.google.gson.Gson.fromJson(Gson.java:768)
04-21 11:16:59.971: E/AndroidRuntime(19633): at com.google.gson.Gson.fromJson(Gson.java:717)
04-21 11:16:59.971: E/AndroidRuntime(19633): at waoo.app.fragment.RechercheResult.saveJson(RechercheResult.java:255)
04-21 11:16:59.971: E/AndroidRuntime(19633): at waoo.app.fragment.RechercheResult$1$1.handleMessage(RechercheResult.java:218)
04-21 11:16:59.971: E/AndroidRuntime(19633): at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 11:16:59.971: E/AndroidRuntime(19633): at android.os.Looper.loop(Looper.java:137)
04-21 11:16:59.971: E/AndroidRuntime(19633): at android.app.ActivityThread.main(ActivityThread.java:5306)
04-21 11:16:59.971: E/AndroidRuntime(19633): at java.lang.reflect.Method.invokeNative(Native Method)
04-21 11:16:59.971: E/AndroidRuntime(19633): at java.lang.reflect.Method.invoke(Method.java:511)
04-21 11:16:59.971: E/AndroidRuntime(19633): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
04-21 11:16:59.971: E/AndroidRuntime(19633): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
04-21 11:16:59.971: E/AndroidRuntime(19633): at dalvik.system.NativeStart.main(Native Method)
04-21 11:16:59.971: E/AndroidRuntime(19633): Caused by: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY at line 1 column 178
04-21 11:16:59.971: E/AndroidRuntime(19633): at com.google.gson.stream.JsonReader.nextString(JsonReader.java:821)
04-21 11:16:59.971: E/AndroidRuntime(19633): at com.google.gson.internal.bind.TypeAdapters$13.read(TypeAdapters.java:358)
04-21 11:16:59.971: E/AndroidRuntime(19633): at com.google.gson.internal.bind.TypeAdapters$13.read(TypeAdapters.java:346)
04-21 11:16:59.971: E/AndroidRuntime(19633): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:93)
04-21 11:16:59.971: E/AndroidRuntime(19633): at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:172)
04-21 11:16:59.971: E/AndroidRuntime(19633): ... 16 more
该模型
public class MRechercheResult {
public String id_profil;
public String id_customer;
public String profil;
public String profil_photo;
public String email;
public String agenda;
public List<Activite> activite;
}
public class Activite {
public String day;
public String from;
public String to;
}
您可以在此处找到json信封:
http://pastie.org/private/ipirfc9pgftd8lmjaoipog
解决方法:
希望对您有所帮助..使用gson时,请务必注意变量名和类型:)
public class MRechercheResult {
public String id_profil;
public String id_customer;
public String profil;
public String profil_photo;
public String email;
public Agenda[] agenda;
public Activite[] activite;
}
public class Activite {
public String day;
public String from;
public String to;
}
public class Agenda {
String day;
String from;
String to;
}
}
标签:android-json,gson,android 来源: https://codeday.me/bug/20191120/2044071.html