如何在Swift中从NSDictionary获取NSDictionary?

时间:2016-09-26 05:18:28

标签: ios swift2 xcode7

我是Swift的新手。我的json详细信息如下。如何获得' assdata'来自AP0 Dictionary.Please简要介绍一步一步。

"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"
}

提前致谢。

2 个答案:

答案 0 :(得分:2)

你可以试试这个。

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)

希望对你有所帮助。

答案 1 :(得分:1)

您的密钥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)
        }
    }
}