简体   繁体   English

将json解析为libgdx中的嵌套ArrayList

[英]Parsing json into a nested ArrayList in libgdx

I was using Jackson for all my json serialisation and deserialisation, but I'm trying to get my game working with GWT now so I'm moving over to the libgdx json parsing libraries. 我在所有的json序列化和反序列化过程中都使用Jackson,但是我现在想让我的游戏与GWT一起使用,因此我将移至libgdx json解析库。

Everything seems ok so far except this 到目前为止,一切似乎还可以

HashMap<String, ArrayList<HighScoreEntry>> high_scores =
              new HashMap<String, ArrayList<HighScoreEntry>>();

The ArrayList within the hashmap is being created as a array of JsonValue rather than an array of HighScoreEntry. 哈希图中的ArrayList被创建为JsonValue数组而不是HighScoreEntry数组。

Can someone explain how I work around this? 有人可以解释我如何解决这个问题吗? I know about json.setElementType(); 我知道json.setElementType(); but can't see how to use it in this instance. 但看不到如何在这种情况下使用它。 I'm playing with writing custom serialisation, but again, I can't work out how to extract exactly what I need. 我正在玩编写自定义序列化程序,但是同样,我无法弄清楚如何准确提取所需的内容。

I'm guessing in a custom serialiser I can use 我猜在可以使用的自定义序列化程序中

    json.readFields(this, jsonData);

to populate everything and then correct the erroneous data afterwards. 填充所有内容,然后再更正错误的数据。

HighScoreEntry class (without methods): HighScoreEntry类(无方法):

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;
}

在此处输入图片说明

Pointers would be appreciated. 指针将不胜感激。

I've worked something out, but I feel like there must be a better way. 我已经解决了一些问题,但是我觉得必须有更好的方法。 If anyone else has any ideas then please post them. 如果其他人有任何想法,请发表。

Adding a custom reader I can correct the corrupted high scores and convert them into instances of HighScoreEntry objects. 添加自定义阅读器,我可以纠正损坏的高分并将其转换为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>> {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM