【问题标题】:LibGDX Json parser - parse specific node/objectLibGDX Json 解析器 - 解析特定节点/对象
【发布时间】:2016-09-14 01:49:45
【问题描述】:

我目前正在我的游戏中实现 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);

【问题讨论】:

  • 您是否尝试过使用 LibGDX 教程来准确地告诉您您的要求? LibGDX Reading & writing JSON,标记部分:阅读对象图,正是您要找的。​​span>
  • @Underbalanced 哦,对不起。我之前读过它,我一定已经浏览了关于 JsonValue 的部分,没有意识到它的真正含义以及它可以用来表示 json DOM。谢谢!

标签: java json parsing libgdx


【解决方案1】:

感谢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());

【讨论】:

    猜你喜欢
    • 2014-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多