LibGDX Json解析器 - 解析特定节点/对象

时间:2016-05-17 19:43:46

标签: java json parsing libgdx

我目前正在游戏中实现JSON解析,但我遇到了一个问题,我无法找到解决方案:如何解析特定节点/对象(不知道该怎么做从JSON文件调用它?让我们说我的JSON看起来像这样:

{
    "intro/credits": { //A node/object.
        "title": "Intro music / Credits music",
        "authors": [
            {
                "name": "Vindsvept",
                "links": {
                    "YouTube": "https://www.youtube.com/channel/UCfSUheoljDlGDjerRylO4Nw",
                    "Bandcamp": "https://vindsvept.bandcamp.com/"
                }
            }
        ]
    },

    "extra": { //Another node/object.
        "title": "extra",
        "authors": [
            {
                "name": "extra",
                "links": {
                    "linkTest": "linkTest"
                }
            }
        ]
    }
}

考虑到JSON,我该怎么做?:

MyObject myObj = json.fromJson(parse.object.called.extra);

1 个答案:

答案 0 :(得分:2)

感谢Underbalanced我现在可以回答我自己的问题:要提取一个名为extra的对象,你会做这样的事情:

Json json = new Json();
JsonValue root = new JsonReader().parse(Gdx.files.internal("path/to/your/file.json"));
JsonValue extra = root.get("extra"); //Replace 'extra' with whatever your object is called.
MyObject myObj = json.fromJson(MyObject.class, extra.toString());