1

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.

Nirav D
  • 71,513
  • 12
  • 161
  • 183
Chandan Jee
  • 5,440
  • 4
  • 16
  • 24
  • 1
    Possible duplicate of [Convert NSDictionary to Swift Dictionary](http://stackoverflow.com/questions/24569447/convert-nsdictionary-to-swift-dictionary) – pedrouan Sep 26 '16 at 05:30
  • In which variable you stored this response and where? Show that part of code. – Bista Sep 26 '16 at 05:32

2 Answers2

2

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.

Sahil_Saini
  • 396
  • 2
  • 15
1

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)
        }
    }
}
Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • Not enter into first if condition – Chandan Jee Sep 26 '16 at 05:30
  • Chandan - there is no loop. – Bista Sep 26 '16 at 05:31
  • @ChandanJee Can you show your json parsing code that will more halpful. – Nirav D Sep 26 '16 at 05:37
  • if let data = data {do { if let jsonResult = try NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers) as? NSDictionary { print("jsonResult = \(jsonResult)") if let jsonStr = jsonResult["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) } }}} } catch let error as NSError { print(error.localizedDescription) }} – Chandan Jee Sep 26 '16 at 05:49
  • Check it @Mr.Nirav D – Chandan Jee Sep 26 '16 at 05:52