Error serializing data received from a swift web service 2

0

Good morning I would like you to help me please, I have a web service in Swift 2 returns an array like that

  

{"message": "Successfully Stored Image." "error": false}

but at the time of serializing it

 do{
     let json = try NSJSONSerialization.JSONObjectWithData(nsdata, options: NSJSONReadingOptions.MutableContainers)
   }catch{
          print(error)
          }

shows me the following error:

  

{Error Domain = NSCocoaErrorDomain Code = 3840 "JSON text did not start   with array or object and option to allow fragments not set. "}

    
asked by Juan David Gil 25.05.2016 в 16:05
source

1 answer

0

On the one hand, the JSON string that you have passed lacks a comma between the end of message and the start of error . That is, you have:

con Exito." "error": false}

and it should be:

con Exito.", "error": false}

On the other hand, fixing that error and leaving the options to zero with [] to me works in a Playground with the following code without any error:

var str = "{\"message\": \"Imagen Almacenada con Exito.\", \"error\": false}"
var nsdata = str.dataUsingEncoding(NSUTF8StringEncoding)!

do {

    let json = try NSJSONSerialization.JSONObjectWithData(nsdata, options: [])

} catch {

    print(error)

}

Update 1

If it does not work, try the AllowFragments option

do {

    let json = try NSJSONSerialization.JSONObjectWithData(nsdata, options: NSJSONReadingOptions.AllowFragments)

} catch {

    print(error)

}
    
answered by 25.05.2016 в 17:20