json - NSJSONSerialization with Swift 2 -


import uikit  let url = nsurl(string: "http://example.com/test.php") let request = nsmutableurlrequest(url: url!)  // modify request necessary, if necessary  let task = nsurlsession.sharedsession().datataskwithrequest(request) {     data, response, error in      if data == nil {         print("request failed \(error)")         return     }      var parseerror: nserror?     if let json = nsjsonserialization.jsonobjectwithdata(data!, options: nil, error: &parseerror) as? [string: string] {         if let name = json["name"] {             print("name = \(name)")   // if good, you'll see "william"         }     } else {         print("parsing error: \(parseerror)")         let responsestring = nsstring(data: data!, encoding: nsutf8stringencoding)         print("raw response: \(responsestring)")     } } task.resume() 

this code worked before update xcode 7 there error in

if let json = nsjsonserialization.jsonobjectwithdata(data!, options: nil, error: &parseerror) as? [string: string] { 

can me convert swift 2?

here go

let url = nsurl(string: "http://example.com/test.php")     let request = nsmutableurlrequest(url: url!)      // modify request necessary, if necessary      let task = nsurlsession.sharedsession().datataskwithrequest(request) {         data, response, error in          if data == nil {             print("request failed \(error)")             return         }          do{             if let json = try nsjsonserialization.jsonobjectwithdata(data!, options: []) as? [string: string] {                 if let name = json["name"] {                     print("name = \(name)")   // if good, you'll see "william"                 }             } else {                 let responsestring = nsstring(data: data!, encoding: nsutf8stringencoding)                 print("raw response: \(responsestring)")             }         }catch {             print("bom bom bom error")         }      }     task.resume() 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -