Json get dynamic property


As an exercise I’m porting a little starling game, of course I got a few problems:

I tried to catch a property the as3 way:

little json example:
“framerate”: 30,
“aliasing”: 2,
“assetpath”: “assets\”,
“mappath”: “map_v3\”,
“stagewidth”: 3840,
“stageheight”: 2160,
“mapwidth”: 14336,
“mapheight”: 5632,
“bgcolor”: “0x9b9b9b”,
“debugS”: true,
“fullscreen”: true,
“mouse”: true,
“timeoutTime”: 300000,
“label0”:"text ecample,
“gal0”: [ “DGF_4042”, “DGF_4049”, “DGF_4058”, “DGF_4063”]
“gal1”: [ “DGF_4042”, “DGF_4049”, “DGF_4058”, “DGF_4063”]

and my code:

var loadedData:String = Assets.getText("assets/content.json");
var jsonObject:Dynamic;
jsonObject = haxe.Json.parse(loadedData);

if (jsonObject.fullscreen== true ) works!

but when I try to do something like this
trace (jsonObject[“gal” +galnr ]);

I can’t compile…

how can I read a dynamic property name, where can I find more informations

Would TJSON be the better solution?

_2 Starling AssetsManager
could I use the new asset folders?

ok, this is the way I found in the starling demo:
_am = new AssetManager();
// ui assets / atlas
Assets.getPath( _jsonObject.assetpath + “ui.xml”),
Assets.getPath( _jsonObject.assetpath + “ui.png”)

but how can I load a complete folder. I would like to switch between different folders during development.

// game assets
var infoAssets : starling.assets.AssetManager = new starling.assets.AssetManager();
 infoAssets .enqueue(_jsonObject.assetpath + "infos/");
 _am.enqueueSingle(infoAssets , "infoAssets");  
_am.loadQueue(onComplete, onError, onProgress);

How can I parse an folder to the required array?


ok, if there’s no way to load complete folders direct, I couls use FileSystem.readDirectory (“assets/infos/” ) in cpp.

how can I fake this in html5. loading an textlist with a filelist?


You could also try Assets.list and use that to find all the asset names that start with the same prefix. There’s more information about reflection here:

I believe this help document describes how to use Reflect.field to read a field dynamically, but also shows an example of using a Haxe typedef to “type” a dynamic object, which tends to be a nicer approach for reading JSON :slight_smile: