如何从 Swift 中的 NSDictionary 获取 NSDictionary?

How to get NSDictionary from NSDictionary in Swift?

我是 Swift.My json detail below.How 的新手,要从 AP0 Dictionary.Please detail 中简要地逐步获取 'assdata' 的字典。

"AP0": {
"assdata": "{
            \"Description\":\"Test\",
            \"Service Requested\":\" Equipment\",
            \"Requested For\":\"Chandan\",
            \"Requested Location\":\"\\Locations\\SBCBSC\\ SBCBSC - MAIN\",
            \"Requested By\":\"Chandan\",
            \"Request Id\":\"100067809074\",
             \"datastatus\":\"true\"}",
"Linked Form": "Equipment",
"System Record ID": "17213450626",
"Submitted By": "Chandan",
"Linked Business Object": "Request",
"Linked Record": "100067809074-0",
"datastatus": "true"
}

提前致谢。

您的密钥 assdata 包含字符串 JSON 响应,因此要从中获取字典,您需要将其转换为第一个数据。

if let jsonStr = yourResponse["assdata"] as? String {
    if let data = jsonStr.dataUsingEncoding(NSUTF8StringEncoding) {
        do {
             let dic = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String:AnyObject]
        } catch let error as NSError {
             print(error)
        }
    }
}

你可以试试这个。

let myJSON =  try NSJSONSerialization.JSONObjectWithData(urlData!, options: NSJSONReadingOptions.MutableContainers) as! NSDictionary

            let keys = myJSON.allKeys
            print(keys)

            let values = myJSON.allValues
            print(values)

            let dict = values[2]
            print(dict)

            let dictAssdata = dict["assdata"]
            print(dictAssdata)

希望对你有帮助。