使用GSON JSON包围Android

使用GSON JSON包围Android,第1张

概述尝试使用下面的2行使用GSON库读取和存储json信封时,应用程序崩溃(请参阅错误日志).解析有什么问题.TypelistType=newTypeToken<ArrayList<MRechercheResult>>(){}.getType();List<MRechercheResult>search_results=newGson().fromJson(envelop,listType);错

尝试使用下面的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: main04-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 17804-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.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 17804-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.read(TypeAdapters.java:358)04-21 11:16:59.971: E/AndroIDRuntime(19633):    at com.Google.gson.internal.bind.TypeAdapters.read(TypeAdapters.java:346)04-21 11:16:59.971: E/AndroIDRuntime(19633):    at com.Google.gson.internal.bind.ReflectiveTypeAdapterFactory.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;    }}
总结

以上是内存溢出为你收集整理的使用GSON JSON包围Android全部内容,希望文章能够帮你解决使用GSON JSON包围Android所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1078655.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存