Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/apache-spark/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Gson序列化/反序列化映射到/来自KeyValuePairs列表_Java_Json_Dictionary_Gson - Fatal编程技术网

Java Gson序列化/反序列化映射到/来自KeyValuePairs列表

Java Gson序列化/反序列化映射到/来自KeyValuePairs列表,java,json,dictionary,gson,Java,Json,Dictionary,Gson,在服务器端,我得到了这个API(示例)(我无法修改它。) 在客户端,我必须创建这些类以进行正确的反序列化: class GetMyObject { @SerializedName("MyDictionary") private List<MyDictionaryItem> myDictionary; } class MyDictionaryItem { @SerializedName("Key") private int key; @Ser

在服务器端,我得到了这个API(示例)(我无法修改它。

在客户端,我必须创建这些类以进行正确的反序列化:

class GetMyObject {
    @SerializedName("MyDictionary")
    private List<MyDictionaryItem> myDictionary;
}

class MyDictionaryItem {
    @SerializedName("Key")
    private int key;

    @SerializedName("Value")
    private int value;
}
类GetMyObject{
@SerializedName(“MyDictionary”)
私有列表myDictionary;
}
类MyDictionaryItem{
@序列化名称(“键”)
私钥;
@序列化名称(“值”)
私有int值;
}
如何配置GSON以简单地使用它:(序列化和反序列化)

类GetMyObject{
@SerializedName(“MyDictionary”)
私人地图字典;
}
它对复杂的键对象更感兴趣,例如:

class ComplexKey {
    @SerializedName("Key1")
    private int key1;

    @SerializedName("Key2")
    private String key2;
}

class GetMyObject {
    @SerializedName("MyDictionary")
    private Map<ComplexKey, Integer> myDictionary;
}
class ComplexKey{
@序列化名称(“键1”)
私钥1;
@序列化名称(“键2”)
私有字符串键2;
}
类GetMyObject{
@SerializedName(“MyDictionary”)
私人地图字典;
}

映射创建自定义
JsonDeserializer


公共类MyDictionaryConverter实现了JsonDeserializer另一个替代方案,Jackson JSON处理器

@JsonDeserialize(contentAs=Integer.class)
private Map<ComplexKey, Integer> myDictionary;
@JsonDeserialize(contentAs=Integer.class)
私人地图字典;

如何使其通用,这样我就不必为每个地图创建了?@GergelyFehérvári我又编辑了一次我的答案。我刚刚尝试过这种方法,它只适用于泛型。
class GetMyObject {
    @SerializedName("MyDictionary")
    private Map<Integer, Integer> myDictionary;
}
class ComplexKey {
    @SerializedName("Key1")
    private int key1;

    @SerializedName("Key2")
    private String key2;
}

class GetMyObject {
    @SerializedName("MyDictionary")
    private Map<ComplexKey, Integer> myDictionary;
}
public class MyDictionaryConverter implements JsonDeserializer<Map<?, ?>> {
    public Map<Object, Object> deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext ctx) {
        Type[] keyAndValueTypes = $Gson$Types.getMapKeyAndValueTypes(typeOfT, $Gson$Types.getRawType(typeOfT));

        Map<Object, Object> vals = new HashMap<Object, Object>();
        for (JsonElement item : json.getAsJsonArray()) {
            Object key = ctx.deserialize(item.getAsJsonObject().get("Key"), keyAndValueTypes[0]);
            Object value = ctx.deserialize(item.getAsJsonObject().get("Value"), keyAndValueTypes[1]);
            vals.put(key, value);
        }
        return vals;
    }
}
gsonBuilder.registerTypeAdapter(new TypeToken<Map>(){}.getType(),
        new MyDictionaryConverter());
@JsonDeserialize(contentAs=Integer.class)
private Map<ComplexKey, Integer> myDictionary;