Java 将json解析为libgdx中的嵌套ArrayList
我使用Jackson进行我所有的json序列化和反序列化,但我现在正试图让我的游戏与GWT一起工作,所以我将转到libgdx json解析库 到目前为止,除了这个,一切都还不错Java 将json解析为libgdx中的嵌套ArrayList,java,json,libgdx,Java,Json,Libgdx,我使用Jackson进行我所有的json序列化和反序列化,但我现在正试图让我的游戏与GWT一起工作,所以我将转到libgdx json解析库 到目前为止,除了这个,一切都还不错 HashMap<String, ArrayList<HighScoreEntry>> high_scores = new HashMap<String, ArrayList<HighScoreEntry>>(); 填充所有内容,然后更正错误数据
HashMap<String, ArrayList<HighScoreEntry>> high_scores =
new HashMap<String, ArrayList<HighScoreEntry>>();
填充所有内容,然后更正错误数据
HighScoreEntry类(无方法):
公共类HighScoreEntry实现可比较的java.io.Serializable{
公共长id;
公众智力得分;
公共字符串语言=“en”;
公共字符串数据;
公共字符串名称;
公共布尔流;
}
我已经想出了一些办法,但我觉得一定有更好的办法。如果其他人有任何想法,请张贴 添加一个自定义阅读器,我可以更正损坏的高分,并将其转换为HighScoreEntry对象的实例
@Override
public void read(Json json, JsonValue jsonData) {
// Read all the fields
json.readFields(this, jsonData);
// replace high scores
HighScoreHashMapJsonValue screwedUpHashMap = json.readValue(HighScoreHashMapJsonValue.class, jsonData.get("high_scores"));
HashMap<String, Array<HighScoreEntry>> newHighScores = new HashMap<String, Array<HighScoreEntry>>();
for (Map.Entry<String, Array<JsonValue>> entry : screwedUpHashMap.entrySet()) {
String key = entry.getKey();
Array<JsonValue> jsonValueHighScores = entry.getValue();
Array<HighScoreEntry> highScoreArray = new Array<HighScoreEntry>();
newHighScores.put(key, highScoreArray);
for (JsonValue highScore : jsonValueHighScores) {
highScoreArray.add(json.readValue(HighScoreEntry.class, highScore));
}
}
high_scores = newHighScores;
}
public static class HighScoreHashMapJsonValue extends HashMap<String, Array<JsonValue>> {
}
@覆盖
公共void读取(Json Json、JsonValue jsonData){
//读取所有字段
readFields(这是jsonData);
//换高分
HighScoreHashMapJsonValue ScreedPhashMap=json.readValue(HighScoreHashMapJsonValue.class,jsonData.get(“高分”);
HashMap newHighScores=新HashMap();
对于(Map.Entry:screwedUpHashMap.entrySet()){
String key=entry.getKey();
数组jsonValueHighScores=entry.getValue();
数组highScoreArray=新数组();
newHighScores.put(键,highscoresarray);
对于(JsonValue highScore:jsonValueHighScores){
add(json.readValue(HighScoreEntry.class,highScore));
}
}
高分=新高分;
}
公共静态类HighScoreHashMapJsonValue扩展HashMap{
}
提供有关HighScoreEntry课程的更多信息please@Tukajo添加了HighScoreEntry。是什么告诉您它是一个JsonValue
而不是HighScoreEntry
?@Tukajo在运行时检查。看到我刚才添加的图像了吗?我不认为这是指ArrayList
?我相信上面的条目是什么?在图像中的Value
下。类型数组
public class HighScoreEntry implements Comparable<HighScoreEntry>, java.io.Serializable {
public long id;
public int score;
public String language = "en";
public String data;
public String name;
public boolean current;
}
@Override
public void read(Json json, JsonValue jsonData) {
// Read all the fields
json.readFields(this, jsonData);
// replace high scores
HighScoreHashMapJsonValue screwedUpHashMap = json.readValue(HighScoreHashMapJsonValue.class, jsonData.get("high_scores"));
HashMap<String, Array<HighScoreEntry>> newHighScores = new HashMap<String, Array<HighScoreEntry>>();
for (Map.Entry<String, Array<JsonValue>> entry : screwedUpHashMap.entrySet()) {
String key = entry.getKey();
Array<JsonValue> jsonValueHighScores = entry.getValue();
Array<HighScoreEntry> highScoreArray = new Array<HighScoreEntry>();
newHighScores.put(key, highScoreArray);
for (JsonValue highScore : jsonValueHighScores) {
highScoreArray.add(json.readValue(HighScoreEntry.class, highScore));
}
}
high_scores = newHighScores;
}
public static class HighScoreHashMapJsonValue extends HashMap<String, Array<JsonValue>> {
}