swift - How to convert a JSON string to a dictionary? -


i want make 1 function in swift project converts string dictionary json format got 1 error:

cannot convert expression's type (@lvalue nsdata,options:integerlitralconvertible ...

this code:

func convertstringtodictionary (text:string) -> dictionary<string,string> {      var data :nsdata = text.datausingencoding(nsutf8stringencoding)!     var json :dictionary = nsjsonserialization.jsonobjectwithdata(data, options:0, error: nil)     return json }  

i make function in objective-c :

- (nsdictionary*)convertstringtodictionary:(nsstring*)string {   nserror* error;   //giving error takes dic, array,etc only. not custom object.   nsdata *data = [string datausingencoding:nsutf8stringencoding];   id json = [nsjsonserialization jsonobjectwithdata:data options:0 error:&error];   return json; } 

warning: convenience method convert json string dictionary if, reason, have work json string. if have json data available, should instead work data, without using string @ all.

swift 3

func converttodictionary(text: string) -> [string: any]? {     if let data = text.data(using: .utf8) {         {             return try jsonserialization.jsonobject(with: data, options: []) as? [string: any]         } catch {             print(error.localizeddescription)         }     }     return nil }  let str = "{\"name\":\"james\"}"  let dict = converttodictionary(text: str) 

swift 2

func convertstringtodictionary(text: string) -> [string:anyobject]? {     if let data = text.datausingencoding(nsutf8stringencoding) {         {             return try nsjsonserialization.jsonobjectwithdata(data, options: []) as? [string:anyobject]         } catch let error nserror {             print(error)         }     }     return nil }  let str = "{\"name\":\"james\"}"  let result = convertstringtodictionary(str) 

original swift 1 answer:

func convertstringtodictionary(text: string) -> [string:string]? {     if let data = text.datausingencoding(nsutf8stringencoding) {         var error: nserror?         let json = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.allzeros, error: &error) as? [string:string]         if error != nil {             println(error)         }         return json     }     return nil }  let str = "{\"name\":\"james\"}"  let result = convertstringtodictionary(str) // ["name": "james"]  if let name = result?["name"] { // `?` here because our `convertstringtodictionary` function returns optional     println(name) // "james" } 

in version, didn't pass proper parameters nsjsonserialization , forgot cast result. also, it's better check possible error. last note: works if value string. if type, better declare dictionary conversion this:

let json = nsjsonserialization.jsonobjectwithdata(data, options: nsjsonreadingoptions.allzeros, error: &error) as? [string:anyobject] 

and of course need change return type of function:

func convertstringtodictionary(text: string) -> [string:anyobject]? { ... } 

Comments