我如何使用" 1"的JSON名称? (或任何其他数字)没有它被归类为Int?

时间:2018-01-17 08:29:32

标签: json haxe

例如,如果我有"all_stations_info": {"1": {"playing": "some song"}, "2": {"playing": "another song"}}并且我希望在电台" 1"上播放这首歌,我怎么做,如果没有" 1"被认可为Int或Float?
typedef不起作用,因为它也不想要Int,我不确定DynamicAccess是否可以用于远程数据。

2 个答案:

答案 0 :(得分:5)

如果您使用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"]);

检查:https://try.haxe.org/#2419b

如果类型定义非常大,那么最好将其定义为typedef:

typedef StationsInfo = { all_stations_info:haxe.DynamicAccess<Any> }

然后您可以使用var json:StationsInfo = haxe.Json.parse('.....

所以,看看你发布的内容,如果你想要它完全输入,那么你可以定义这样的类型:

typedef StationsInfo = { all_stations_info:haxe.DynamicAccess<StationData> };
typedef StationData = { playing: String };

检查:https://try.haxe.org/#C6F6d

答案 1 :(得分:1)

您是否尝试过使用Reflect

可在此处找到示例:https://try.haxe.org/#c621C