如何在Swift中解析JSON

时间:2017-05-06 14:36:32

标签: json swift parsing

我有JSON代码,我需要在swift语言上解析

{
    data =     {
        id = "kUu6AXbbN2tEeCJJ22fdHh";
        name = Ralf;
    };
}

和这段代码

if let myData = data{
    do{
    let myJson = try JSONSerialization.jsonObject(with: myData, options: 
JSONSerialization.ReadingOptions.mutableContainers) as AnyObject}
                    catch{    
                    }

在命令行中,我可以看到我的json结构和里面的内容,但我无法理解如何解析它。有人可以帮助我,我只是开始使用swift

1 个答案:

答案 0 :(得分:-2)

}

            if let myData = data{
                do{
                    let myJson = try JSONSerialization.jsonObject(with: myData, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
                    if let data = myJson["data"] as AnyObject? {
                        if let id = data["id"] as! NSString? {
                            print (id)
                        }
                        if let name = data["name"] as! NSString? {
                            print (name)
                        }
                    }
                }

                catch{

                }
                }