gpt4 book ai didi

json - 如何使用 LibGDX 解析这个 JSON

转载 作者:行者123 更新时间:2023-12-04 22:29:34 24 4
gpt4 key购买 nike

 "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"
}
]

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

我知道我可以去掉“类”处理并简单解析 JSON,但我需要能够使用 LibGDX 来处理这个问题。

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

谢谢。

最佳答案

您可以使用 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");
}

关于json - 如何使用 LibGDX 解析这个 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35343727/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com