gpt4 book ai didi

android - 使用GSON对Android进行json封装

转载 作者:可可西里 更新时间:2023-10-31 22:04:23 25 4
gpt4 key购买 nike

在尝试使用以下两行使用 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 - 使用GSON对Android进行json封装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29765795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com