在swift中反序列化JSON

时间:2016-10-29 00:00:22

标签: ios json swift json-deserialization

有没有办法在swift中正确反序列化这个结构的json?

{
Usuario =     (
            {
        "picture_url" = "";
        id = 229;
        name = "ABC";
    }
);}

我用jsonHelper库测试了这个

let jsonData = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers )
let jsonString = jsonData
var mstemp = [usuario]()
mstemp <-- jsonString

但只适用于jsons这个结构

{"name": "myUser", "id": "1","picture_url": ""}

更新

代码:

let urlFinal = URLSERVIDOR+"/ws/rest/Geral/consultaUsuario?token=\(validotoken)&email=\(validoEmail)&senha=\(SenhaCriptada)"
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:NSJSONReadingOptions.MutableContainers )
        let jsonString = jsonData
        var mstemp = [usuario]()
        mstemp <-- jsonString
        dispatch_async(dispatch_get_main_queue(),{
            sucesso(usuarioBaixado: mstemp)
        });
    } catch _ {
        falha()
    }
}
task.resume()

1 个答案:

答案 0 :(得分:0)

试试这个:

[^)]+