LibGDX Json解析器 - 解析特定节点/对象
问题描述:
我目前在我的游戏中实现了JSON解析,但我遇到了一个问题,我找不到解决方案:如何解析特定节点/对象(不是确定要调用它)来自JSON文件?比方说,我的JSON是这样的:LibGDX 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);
答
感谢Underbalanced我现在可以回答我的问题:以exctract一个叫做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());
你有没有试过使用LibGDX教程,告诉你到底你在问什么? [LibGDX读写JSON](https://github.com/libgdx/libgdx/wiki/Reading-&-writing-JSON),章节标记:读对象图,就是你要找的东西。 – Underbalanced
@不平衡哦,对不起。我之前阅读过这篇文章,并且我必须详细介绍关于JsonValue的部分,而不是意识到它的真正含义,并且可以用它来表示json DOM。谢谢! – Charanor