简体   繁体   English

如何在Swift中从NSDictionary获取NSDictionary?

[英]How to get NSDictionary from NSDictionary in Swift?

I am new in Swift.My json detail below.How to get dictionary of 'assdata' from AP0 Dictionary.Please detail briefly and step by step. 我是Swift的新手。我的json详细信息如下。如何从AP0 Dictionary获取'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"
}

Thanks In Advance. 提前致谢。

You can try this. 你可以试试这个。

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)

Hope it help you. 希望它对你有所帮助。

Your key assdata contains String JSON response so that for getting Dictionary from it you need to convert it to first data. 您的密钥assdata包含String JSON响应,因此要从中获取Dictionary,您需要将其转换为第一个数据。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM