我是gson的新手。我想将json属性反序列化为java对象中的hashmaps,并且有许多这样的jsons被加入到一堆对象中。 例如,jsons是一个数组;看起来像,
[
{"type":"type1","name":"name1","attr1":123},
{"type":"type1","name":"name1","attr2":123},
{"type":"type1","name":"name1","attr3":123},
{"type":"type1","name":"name1","attr1":123},
{"type":"type1","name":"name1","attr2":123},
{"type":"type1","name":"name1","attr3":123}
]
请注意,jsons有一些常见的属性名称(类型和名称),有些属性名称不同(attr1,attr2,attr3等)
我想将json中的这个属性反序列化为java对象中的Map,
public class Object {
Map<String, Integer> props_ = new HashMap<>();
public boolean has(String prop) {
return props_.containsKey(prop);
}
public Integer get(String prop) {
return props_.get(prop);
}
public void set(String prop, Integer value) {
props_.put(prop, value);
}
}
所以props_ map应该有第一个json的类型,名称和attr1,以及 第二个json等的类型,名称和attr2