简体   繁体   中英

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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