Swift json deserialize

Asked

Viewed 103 times

0

Hello, I have the following code to deserialize a json:

let urlFinal = URLSERVIDOR+"/Geral/consulta?idcliente=\(id)"
        let jsonUrl = urlFinal
        let session = NSURLSession.sharedSession()
        let shotsUrl = NSURL(string: jsonUrl)

        let task = session.dataTaskWithURL(shotsUrl!) {data, response, error in
            guard data != nil else {
                falha()
                return
            }

            do {
                let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String: AnyObject]
                let J = jsonData as! NSDictionary
                let us = J["ServicoCliente"]
                var mstemp = [servicocliente]()
                mstemp <-- us
                dispatch_async(dispatch_get_main_queue(),{
                    sucesso(servs: mstemp)
                });
            } catch _ {
                print("##")
                falha()
            }
        }
        task.resume()

It works very well, but in some specific jsons it has been showing error, failing in line

let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? [String: AnyObject]

I use the jsonHelper library, but it fails even before in the part that uses it (var mstemp = servicocliente; mstemp <-- us).

I cannot find the reason for the failure, the json they are downloading is as follows:

{"ServicoCliente":[{"id":"190","cliente":"229","enderecoFavoritos":"41","servicoProfissional":"191","ind_estado_cliente":"A","ind_estado_profissional":"","profissional_id":"230","profissional_nome":"Athila Zuma","servicotipo_nome":"Clínica / Hospital","servicosubtipo_nome":"Consulta","dta_inc_alt":"2016-10-24 20:59:43.473","ind_finalizado":"N"},{"id":"192","cliente":"229","enderecoFavoritos":"46","servicoProfissional":"194","ind_estado_cliente":"A","ind_estado_profissional":"","profissional_id":"240","profissional_nome":"Max","servicotipo_nome":"shop","servicosubtipo_nome":"Tosa ","dta_inc_alt":"2016-11-03 00:54:58.682","ind_finalizado":"N"},{"id":"193","cliente":"229","enderecoFavoritos":"47","servicoProfissional":"195","ind_estado_cliente":"O","ind_estado_profissional":"O","profissional_id":"243","profissional_nome":"Ultra","servicotipo_nome":"Clínica / Hospital","servicosubtipo_nome":"Emergência 24H","dta_inc_alt":"2016-11-05 11:12:34.536","ind_finalizado":"Y"}]}

How could I get this information without causing this error?

  • Maybe it is pq this json is not using the correct Unicode to escape some characters, already tried to send the json without these special chars?

  • I don’t have access to the server, can you handle it in Swift? @Jancássio

  • Athila, has a repository on Github demonstrating the operation of an entire JSON app. I think it can be quite useful for you. See: https://github.com/ethanwang/Artistry I hope I helped. Hugs!

1 answer

1

Try to use:

    public class func serializeJSON(dictionary: NSDictionary) -> NSData? {
        var data: NSData? = nil
        do {
        data = try NSJSONSerialization.dataWithJSONObject(dictionary, options: NSJSONWritingOptions.PrettyPrinted)
        } catch let error as NSError {
            print("SMCore ->  Error JSON Serialization: \(error) --<<<", terminator: "")
        }
        return data
    }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.