ios - 如何在 Swift 中将所有键和值从 NSDictionary 中获取到单独的字符串数组中?

标签 ios arrays json swift

    let urlAsString = "https://drive.google.com/uc?export=download&id=0B2bvUUCDODywWTV2Q2IwVjFaLW8"
    let url = NSURL(string: urlAsString)!
    let urlSession = NSURLSession.sharedSession()

    let jsonQuery = urlSession.dataTaskWithURL(url, completionHandler: { data, response, error -> Void in
        do {
            if let jsonDate = data, let jsonResult = try NSJSONSerialization.JSONObjectWithData(jsonDate, options: []) as? NSDictionary {

                print(jsonResult)

            }
        } catch let error as NSError {
            print(error)
        }


    })
    jsonQuery.resume()

好的,所以我在这里从在线 json 接收数据,然后将其作为 NSDictionary 存储在 jsonresult 中。我需要将所有键和值放入两个单独的数组中?

基本上我想要这个

jsonresult.allkeys --> 字符串数组
jsonresult.allvalues --> 字符串数组

最佳答案

您可以使用:

let keys = jsonResult.flatMap(){ $0.0 as? String }  
let values = jsonResult.flatMap(){ $0.1 }  

关于ios - 如何在 Swift 中将所有键和值从 NSDictionary 中获取到单独的字符串数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38811930/

相关文章:

ios - 在实例化 UISwitcher 时尝试实例化 UILabel

ios - 如何减小 UITableViewCell 中 ContentSize 的宽度

c - 从两个数组中取出正数并从中创建第三个

java - 列表问题 - 当我运行它时,为什么它在第三次打印输出上打印出-4?

php - 如何编辑使用 json_decode() 创建的 PHP 对象?

ios - Swift - 用于打开 PDF 文件的应用程序扩展

objective-c - 将 UIPickerView 设置为 UITableview 的底部,无论滚动位置如何

java - 查找多维数组的所有垂直遍历

php - 如何在 JSON 中编码 MYSQL 响应并在 iOS 应用程序中解析

javascript - jQuery 循环遍历 JSON 对象