SwiftyJSON-迭代ID不是数组的对象

时间:2017-03-10 00:49:50

标签: json swift swifty-json

我正在向一个API发出请求,该API有时会返回一个简单的JSON对象数组,我正在使用一个简单的"解析我的数量"循环,我使用SwiftyJSON追加json [i] [" city"]。例如:

[{"city":"Lakefront","code":"NEW","country":"United States","municipality":"New Orleans","isChild":false,"hasChild":false},
{"city":"Auckland - Auckland International Airport","code":"AKL","country":"New Zealand","municipality":"Auckland","isChild":false,"hasChild":false},
{"city":"Blenheim","code":"BHE","country":"New Zealand","municipality":"Blenheim","isChild":false,"hasChild":false}]

但是,在某些情况下,API将返回一个具有多对键和对象值的对象,我在使用SwiftyJSON解析时遇到问题。例如:

{"2":{"city":"New York","code":"NYC","country":"United States","municipality":"New York","isChild":false,"hasChild":true},
"32":{"city":"John F. Kennedy -  NY","code":"JFK","country":"United States","municipality":"New York","isChild":true,"hasChild":false},
"414":{"city":"LaGuardia -  NY","code":"LGA","country":"United States","municipality":"New York","isChild":true,"hasChild":false}}

在第二种情况下,是否有办法使用SwiftyJSON循环遍历第一个对象,并获取对象值,注意我不会事先知道ID(2,32,414)?

谢谢!

1 个答案:

答案 0 :(得分:0)

在你的第一个案例中,你有一系列词典。在第二种情况下,您有一本词典字典。我没有长时间使用SwiftyJSON,我不记得它是如何工作的,但这应该足以让你去。