2016-05-17 55 views
-1

我目前在我的遊戲中實現了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); 
+0

你有沒有試過使用LibGDX教程,告訴你到底你在問什麼? [LibGDX讀寫JSON](https://github.com/libgdx/libgdx/wiki/Reading-&-writing-JSON),章節標記:讀對象圖,就是你要找的東西。 – Underbalanced

+0

@不平衡哦,對不起。我之前閱讀過這篇文章,並且我必須詳細介紹關於JsonValue的部分,而不是意識到它的真正含義,並且可以用它來表示json DOM。謝謝! – Charanor

回答

2

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