json - 我正在尝试使用循环获取存储在 JSONData 中的轨道名称

标签 json swift api

这是代码。

func callFire(url:String) {
    Alamofire.request(url).responseJSON(completionHandler: {response in
        self.parseData(JSONData: response.data!)
    })
}

func parseData(JSONData: Data) {
    do {
        var readableJSON = try JSONSerialization.jsonObject(with: JSONData, options: .mutableContainers) as! JSONStandard
        if let tracks = readableJSON["tracks"] as? JSONStandard {
            if let items = tracks["items"]{
                for i in 0..<items.count {
                    let item = items[i] as! JSONStandard
                    let name = item["name"] as! String
                    trackNames.append(name)
                    self.tableView.reloadData()
                }
            }
        }
    }
    catch {
        print(error)
    }
}

这就是我收到错误“'下标'的使用不明确”的地方

令 item = items[i] 为! JSON标准

最佳答案

为什么使用 JSONStandard?只需将该节点转换为 Map 或 Array 即可。如果它是 map ,你会这样做:

if let tracks = readableJSON["tracks"] as? [String: Any]

然后你可以按名称提取内容,例如:

let name = tracks["name"] as? String

顺便说一句,这是来自 this guide Apple 最近发布了有关处理 JSON 的信息。

关于json - 我正在尝试使用循环获取存储在 JSONData 中的轨道名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39560316/

相关文章:

Python 映射列表中的变量

ios - 如何在 Swift 中参数化变量?

api - 如何在 API 中获取 linke 的 r_basicprofile 访问权限

json - 如何在服务器端最好地验证JSON

java - 将 Decimal128 序列化为 JSON

Python:添加到列表中的字典

ios - Xcode 8/Swift 3 : how to make ViewController save state when segue occurs?

android - RxAndroid : Possible to use zip function on an array of api requests with interval or delay?

javascript - Backbone : Create collection from JSON

swift - NSTextField - 仅允许使用强制触摸、Cocoa 进行编辑