EASY VERSIONConvert json zu Map.Entry Objekt mit Gson
Wenn ich frage Gson einige gültige Json konvertieren myMap es kein Problem hat es
public class MyMap{
Map<Long,String> content;
}
MyMap myMap = gson.fromJson(json, new TypeToken<MyMap>() {}.getType());
HARD VERSION tun:
Wie Bekomme ich Gson, um folgendes zu tun?
public class MyDS{
Map<Map.Entry<Long,String>,Map<Long,String>> content;
}
MyDS myDS = gson.fromJson(json, new TypeToken<MyDS>() {}.getType());
Beispiel json, wenn Sie es wirklich brauchen.
"content": {
"[1, dog]": {
"1": "max",
"2": "pi",
"3": "robot",
"4": "catcher",
"5": "reaper"
},
"[2, cat]": {
"6": "black",
"7": "white",
"8": "meow",
"9": "mice",
"10": "rat"
},
"[3, rabbit]": {
"16": "bunny",
"17": "ears",
"28": "burgerbun",
"39": "alice",
"50": "tweak"
}
}
mehr Noten
Für eine gute Maßnahme, ich versuche, einen Komponententest zu laufen, wo alles, was ich versuche zu tun ist, um die json mit Gson zu lesen, und ich erhalte die folgende Fehlertrace:
at sun.misc.Unsafe.allocateInstance(Native method)
java.lang.reflect.Method.invoke!(Native method)
com.google.gson.internal.UnsafeAllocator$1.newInstance(UnsafeAllocator.java:48)
com.google.gson.internal.ConstructorConstructor$14.construct(ConstructorConstructor.java:223)
com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.read(ReflectiveTypeAdapterFactory.java:207)
com.google.gson.internal.bind.TypeAdapterRuntimeTypeWrapper.read(TypeAdapterRuntimeTypeWrapper.java:40)
com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:186)
com.google.gson.internal.bind.MapTypeAdapterFactory$Adapter.read(MapTypeAdapterFactory.java:145)
com.google.gson.Gson.fromJson(Gson.java:861)
com.google.gson.Gson.fromJson(Gson.java:826)
com.google.gson.Gson.fromJson(Gson.java:775)
Es spielt keine Rolle, wenn die Schlüssel der Form "[3, rabbit]"
für "{3, rabbit}"
Können Sie das Beispiel json für dieses zeigen? – notionquest
Fixieren Sie Ihre Angebote. – shmosel
Die Zitate sind nur so, weil ich in einen TextEditor einfügen. Aber es geht ihnen gut. Wie gesagt, es funktioniert, bis ich einen Eintrag für den Schlüssel brauche. –