例如,如果我有"all_stations_info": {"1": {"playing": "some song"}, "2": {"playing": "another song"}}并且我想让电台在电台"1"播放,那么如果没有将"1"识别为Int或Float,我怎么做?
typedef不起作用,因为它也不需要Int,我不确定DynamicAccess是否可以用于远程数据.
如果您使用haxe.DynamicAccess<Any>类型,那么您可以像这样使用数组访问all_stations_info["1"].
DynamicAccess是一种抽象类型,用于处理匿名结构,这些结构旨在通过字符串键来保存对象集合.DynamicAccess在内部使用Reflect.
var json:{ all_stations_info:haxe.DynamicAccess<Any> } = haxe.Json.parse('{"all_stations_info": {"1": {"playing": "some song"}, "2": {"playing": "another song"}}}');
trace(json.all_stations_info["1"]);
Run Code Online (Sandbox Code Playgroud)
检查:https://try.haxe.org/#2419b
如果类型定义非常大,那么最好将其定义为typedef:
typedef StationsInfo = { all_stations_info:haxe.DynamicAccess<Any> }
Run Code Online (Sandbox Code Playgroud)
然后你可以使用 var json:StationsInfo = haxe.Json.parse('.....
所以,看看你发布的内容,如果你想要它完全输入,那么你可以定义这样的类型:
typedef StationsInfo = { all_stations_info:haxe.DynamicAccess<StationData> };
typedef StationData = { playing: String };
Run Code Online (Sandbox Code Playgroud)
检查:https://try.haxe.org/#C6F6d
| 归档时间: |
|
| 查看次数: |
79 次 |
| 最近记录: |