将json解析为libgdx中的嵌套ArrayList [英] Parsing json into a nested ArrayList in libgdx

查看:98
本文介绍了将json解析为libgdx中的嵌套ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用Jackson进行所有的json序列化和反序列化,但是我现在想让我的游戏与GWT一起使用,因此我将移至libgdx json解析库.

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.

到目前为止,一切似乎还可以,

Everything seems ok so far except this

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

哈希表中的ArrayList被创建为JsonValue数组而不是HighScoreEntry数组.

The ArrayList within the hashmap is being created as a array of JsonValue rather than an array of HighScoreEntry.

有人可以解释我如何解决此问题吗?我知道json.setElementType();但看不到如何在这种情况下使用它.我正在编写自定义序列化程序,但是同样,我无法弄清楚如何准确提取所需的内容.

Can someone explain how I work around this? I know about 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类(无方法):

HighScoreEntry class (without methods):

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

指针将不胜感激.

推荐答案

我已经解决了一些问题,但是我觉得必须有更好的方法.如果其他人有任何想法,请发表.

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.

添加自定义阅读器,我可以纠正损坏的高分并将其转换为HighScoreEntry对象的实例.

Adding a custom reader I can correct the corrupted high scores and convert them into instances of HighScoreEntry objects.

@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>> {

}

这篇关于将json解析为libgdx中的嵌套ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆