JSON Parsing in Swift 3

前端 未结 8 794
暖寄归人
暖寄归人 2020-12-01 11:05

Has anyone been able to find a way to parse through JSON files in Swift 3? I have been able to get the data to return but I am unsuccessful when it comes to breaking the dat

8条回答
  •  离开以前
    2020-12-01 11:15

     override func viewDidLoad() {
            super.viewDidLoad()
            let url=URL(string:"http://api.androidhive.info/contacts/")
            do {
                let allContactsData = try Data(contentsOf: url!)
                let allContacts = try JSONSerialization.jsonObject(with: allContactsData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String : AnyObject]
                if let arrJSON = allContacts["contacts"] {
                    for index in 0...arrJSON.count-1 {
                        let aObject = arrJSON[index] as! [String : AnyObject]
                        names.append(aObject["name"] as! String)
                        contacts.append(aObject["email"] as! String)
                    }
                }
                print(names)
                print(contacts)
                self.tableView.reloadData()
            }
            catch {
            }
        }
    

提交回复
热议问题