如何在Swift中从NSDictionary获取NSDictionary



我是Swift的新手。下面是我的json细节。如何从AP0字典获得'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"
}

你可以试试这个

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)

希望对你有帮助。

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

最新更新