我有这个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这样的属性时?
答案 0 :(得分:1)
TL; DR你需要一个适配器。
Gson可以在没有显式适配器的情况下使用Map
,但它只是期望密钥为String
,因为它将地图转换为JSON对象。如果它不是String
,则会将其转换为通常只是调用Object#toString()
。
另一种表达方式是:对象的JSON表示如何?不明显,因为你不能将复杂的对象作为JSON对象的属性,它们总是字符串。
因此,您需要确定您的代表和为其编写自定义适配器。
作为奖励我可以建议你在类似数组的东西中思考来表示你的pathData。您可以编写一个对象数组来代替映射,这些对象是键/值对。像这样:
{
"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"
}
}
]
}
注意:不是一个完整的例子,只是序列化而没有太多检查等等。仅作为示例有效。
上一代码的输出是:
img