gpt4 book ai didi

java - 如何使用 GSON 序列化和反序列化包含 HashMap 和 Pairs 的对象

转载 作者:行者123 更新时间:2023-11-30 10:46:08 25 4
gpt4 key购买 nike

我有这个 Hashmap Map<Pair<String,String>,Map<String,String>> pathData = new HashMap<>();作为其他对象 (Tour_Object) 的属性,我正在尝试使用 GSON 库在 json 中对其进行序列化/反序列化。

public static String setTourToJson(Tour_Object tourObject)
{
Gson gson = new Gson();
return gson.toJson(tourObject);
}

public static Tour_Object getTourFromJson(String JsonString)
{
Gson gson = new Gson();
return gson.fromJson(JsonString, new TypeToken<Tour_Object>() {
}.getType());
}

当我进行反序列化时抛出以下异常:

04-19 11:18:49.449 29076-29076/abff.fxguide E/AndroidRuntime: FATAL EXCEPTION: main
Process: abff.fxguide, PID: 29076
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 100 path $.pathData.
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:220)
at com.google.gson.Gson.fromJson(Gson.java:879)
at com.google.gson.Gson.fromJson(Gson.java:844)
at com.google.gson.Gson.fromJson(Gson.java:793)
at com.google.gson.Gson.fromJson(Gson.java:765)
at abff.fxguide.Tour_Helper.getTourFromJson(Tour_Helper.java:311)
at abff.fxguide.Tour_All.showTourDetails(Tour_All.java:266)
at abff.fxguide.Tour_All$8.onClick(Tour_All.java:370)
at android.view.View.performClick(View.java:4856)
at android.view.View$PerformClick.run(View.java:19956)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5389)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 100 path $.pathData.
at com.google.gson.stream.JsonReader.beginObject(JsonReader.java:388)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:183)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
at com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:186)
at com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$1.read(ReflectiveTypeAdapterFactory.java:116)
at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:216)
at com.google.gson.Gson.fromJson(Gson.java:879) 
at com.google.gson.Gson.fromJson(Gson.java:844) 
at com.google.gson.Gson.fromJson(Gson.java:793) 
at com.google.gson.Gson.fromJson(Gson.java:765) 
at abff.fxguide.Tour_Helper.getTourFromJson(Tour_Helper.java:311) 
at abff.fxguide.Tour_All.showTourDetails(Tour_All.java:266) 
at abff.fxguide.Tour_All$8.onClick(Tour_All.java:370) 
at android.view.View.performClick(View.java:4856) 
at android.view.View$PerformClick.run(View.java:19956) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:211) 
at android.app.ActivityThread.main(ActivityThread.java:5389) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

我想知道主对象 (Tour_Object) 是否可反序列化,如 getTourFromJson(String JsonString) 所示。当使用像 pathData 这样的属性时?

最佳答案

TL;DR 你需要一个适配器。

Gson 可以与 Map 一起工作没有明确的适配器,但它只是希望 key 是 String因为它将 map 转换为 JSON 对象。如果它不是 String它将它转换为通常只调用 Object#toString() 的一个.

另一种说法是:对象的 JSON 表示形式如何?不明显,因为您不能将复杂对象作为 JSON 对象的属性,它们始终是字符串。

因此您需要确定您的表示为其编写自定义适配器。

延长奖金答案

作为奖励,我可以建议您考虑用数组之类的东西来表示您的路径数据。您可以编写一个对象数组,而不是映射,这些对象是键/值对。像这样:

{
"pathData": [
{
"pair": {"a": "one", "b": "two"},
"map": { ... }
},
{
"pair": {"a": "one", "b": "two"},
"map": { ... }
}
]
}

在哪里pair是你的 Pair<String,String>map是你的 Map<String,String> .

然后你可以写TypeAdapter像这样。

package net.sargue.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class SO36716159 {
public static void main(String[] args) {
Tour_Object o = new Tour_Object();
o.pathData.put(new Pair<>("one", "two"), Collections.emptyMap());
Map<String, String> myMap = new HashMap<>();
myMap.put("a", "b");
myMap.put("c", "d");
o.pathData.put(new Pair<>("three", "four"), myMap);

Gson gson = new GsonBuilder()
.registerTypeAdapter(Tour_Object.class,
new TourObjectAdapter())
.setPrettyPrinting()
.create();
String json = gson.toJson(o);
System.out.println("json = " + json);
}

public static class Pair<A, B> {
private A a;
private B b;

public Pair(A a, B b) {
this.a = a;
this.b = b;
}
}

public static class Tour_Object {
private Map<Pair<String, String>, Map<String, String>> pathData = new HashMap<>();

public Map<Pair<String, String>, Map<String, String>> pathData() {
return pathData;
}
}

public static class TourObjectAdapter extends TypeAdapter<Tour_Object> {
@Override
public void write(JsonWriter out, Tour_Object value)
throws IOException
{
Gson gson = new Gson();

out.beginObject()
.name("pathData")
.beginArray();

for (Map.Entry<Pair<String, String>, Map<String, String>> entry :
value.pathData().entrySet()) {

out.beginObject();
out.name("pair");
gson.getAdapter(new TypeToken<Pair<String,String>>() {})
.write(out, entry.getKey());
out.name("map");
gson.getAdapter(new TypeToken<Map<String, String>>() {})
.write(out, entry.getValue());
out.endObject();
}
out.endArray()
.endObject();
}

@Override
public Tour_Object read(JsonReader in) throws IOException {
throw new RuntimeException("Left as an exercise for the reader... ;-)");
}
}
}

注意:不是一个完整的例子,只是序列化,没有太多检查等等。仅作为示例有效。

前面代码的输出是:

json = {
"pathData": [
{
"pair": {
"a": "one",
"b": "two"
},
"map": {}
},
{
"pair": {
"a": "three",
"b": "four"
},
"map": {
"a": "b",
"c": "d"
}
}
]
}

关于java - 如何使用 GSON 序列化和反序列化包含 HashMap 和 Pairs 的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36716159/

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