json - 为什么我的 commonName 在我的循环中重复相同的内容?

标签 json swift loops for-loop

from this link我得到了相同的 commonName 但它应该是不同的?!

let jsonData = NSData(contentsOfURL: url!)
let readableJSON = JSON(data: jsonData!, options: NSJSONReadingOptions.MutableContainers, error: nil)
let object = try! NSJSONSerialization.JSONObjectWithData(jsonData!, options: NSJSONReadingOptions.AllowFragments) as! Dictionary<String, AnyObject>
let json = JSON(object)

for (_,object):(String, JSON) in readableJSON { 
    let commonName = json["toLocationDisambiguation"]["disambiguationOptions"][0]["place"]["commonNam‌​e"].stringValue 
    commonNameArray.append(commonName)
}

最佳答案

因为 [0],你需要做一些类似的事情

var i = 0
for (_,object):(String, JSON) in readableJSON { 
    let commonName = json["toLocationDisambiguation"]["disambiguationOptions"][i]["place"]["commonNam‌​e"].stringValue 
    commonNameArray.append(commonName)
i = i+1
}

关于json - 为什么我的 commonName 在我的循环中重复相同的内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35582252/

相关文章:

java - 有什么方法可以使用 Swift 进行 iOS 和 Android 开发吗?

javascript - 为什么这个 json 交换代码不起作用?

python - Aiohttp 尝试获取带有页码的页面响应,直到达到空响应

php - MYSQL 查询 - 我的大脑爆炸了

ios - 在 iOS 中开发家长应用

ios - Swift:成功登录后转到下一个屏幕

ios - 如何创建一系列函数并在 Swift 中循环它们?

c - 之后使用 rand() 和 srand(time(NULL)) 不会生成新的随机数

java - 用一小段 Java 代码将 343 更改为 42

javascript - 在ajax中读取对象数组的数组