Java Json使用Gson序列化和反序列化复杂内容
我有一个我想用JSON存储在文件中的结构。没有一个实现类比给定的信息更重要Java Json使用Gson序列化和反序列化复杂内容,java,json,serialization,gson,deserialization,Java,Json,Serialization,Gson,Deserialization,我有一个我想用JSON存储在文件中的结构。没有一个实现类比给定的信息更重要 public class ItemExample implements IItem{ private ModelMap map; private String name; } public class ModelMap { private HashMap<Coord, IPartType> map; } public class Coord { private int x,y,z;
public class ItemExample implements IItem{
private ModelMap map;
private String name;
}
public class ModelMap {
private HashMap<Coord, IPartType> map;
}
public class Coord {
private int x,y,z;
}
public class PartExample implements IPartType {
private String name;
private Purity purity;
}
public Enum Purity{
}
你能提供数据样本吗?用可能的JSON示例更新帖子。内部“地图”的内容永远不会少于20个条目,最多可以有4096个条目。此信息仅在启动和关闭时使用。您是否尝试过
gson.fromJson(jsonStringData,YourClass.class)
+@SerializedName
?我没有,“+@SerializedName”做了什么SerializedName
解决了POJO和传入JSON数据之间的命名冲突,即name(您的类)和“name”(疯狂的JSON)
public class PartDeserialize<T> implements JsonDeserializer<T>{
@Override
public T deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject content = json.getAsJsonObject();
String name = content.get("name").getAsString();
Purity purity = Purity.valueOf(content.get("purity").getAsString());
return new Gson().fromJson(content, typeOfT);
}
}
{
"name" : "sword",
"map" :
{
"map" :
{
"1,1,1" : //string representation of Coord
{
"name" : "blade",
"purity" : "base" // string representation of Purity Enum
},
"0,0,0" :
{
"name" : "handle",
"purity" : "high"
}
}
}
}