如何使用LibGDX解析此JSON

far*_*ord 4 json libgdx

 "components":[  
        {  
           "class":"AssetReference",
           "asset":{  
              "class":"TextureRegionAsset",
              "relativePath":"gfx/opengraph.png"
           }
        },
        {  
           "class":"Layer"
        },
        {  
           "class":"ProtoVisSprite",
           "width":5,
           "height":5
        },
        {  
           "class":"Transform",
           "x":0.13817275,
           "y":2.8430145,
           "scaleX":0.2,
           "scaleY":0.2
        },
        {  
           "class":"Origin"
        },
        {  
           "class":"Tint"
        },
        {  
           "class":"Renderable",
           "zIndex":2
        },
        {  
           "class":"VisID",
           "id":"scratch"
        }
     ]
Run Code Online (Sandbox Code Playgroud)

我在使用LibGDX解析嵌套资产时遇到了一些问题.有谁知道如何使用TextureRegionAsset中的relativePath将资产分配给AssetReference?

我知道我可以删除"类"处理并简单解析JSON,但我需要能够使用LibGDX来处理它.

理想情况下,我希望解析数据并从JSON创建一个精灵.

谢谢.

Mad*_*nyo 9

你可以使用JsonReader并获得JsonValue它.

JsonReader json = new JsonReader();
JsonValue base = json.parse(Gdx.files.internal("file.json"));

//print json to console
System.out.println(base);

//get the component values
JsonValue component = base.get("components");

//print class value to console
System.out.println(component.getString("class"));

//array objects in json if you would have more components
for (JsonValue component : base.get("components"))
{
    System.out.println(component.getString("class"));
    System.out.println(component.get("asset").getString("class");
    System.out.println(component.get("asset").getString("relativePath");
}
Run Code Online (Sandbox Code Playgroud)