使用SwiftyJSON解析JSON

时间:2017-01-22 21:32:34

标签: ios json swift uitableview

我正在开发一个项目,我从网站上获取的JSON文件没有任何(密钥)供我开始解析。因此,当我在代码中使用该行时,let userInfo = swiftyJSON[].arrayValue我将从JSON文件中获取所有信息。我的问题是我需要进一步解析它,所以我可以填写一个UITableView,我似乎无法解析它更具体。

这是我的JSON

中的代码
let externalURL = "http://jsonplaceholder.typicode.com/users"

func getJSON(){
            let url = URL(string: externalURL)
            let request = NSURLRequest(url: url! as URL)
            let session = URLSession(configuration:URLSessionConfiguration.default)
            let task = session.dataTask(with: request as URLRequest) {(data, response, error) -> Void in
                if error == nil {
                    let swiftyJSON = JSON(data: data!)
                    let userInfo = swiftyJSON[].arrayValue
                    print(userInfo)
    }
                else{
                    print("There was an error")
                }
            }
            task.resume()
        }

1 个答案:

答案 0 :(得分:0)

一个起点:

let swiftyJSON = JSON(data: data!)
if let userInfo = swiftyJSON.array {
   for user in userInfo {
     print(user["name"].string, user["email"].string, user["phone"].string)
     if let address = user["address"].dictionary {
         print(address["city"]?.string)
     }
   }
}

所有印刷值均为选项。