So, how to parse following type of JSON?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
"contacts": [ { "id": "c200", "name": "Ashish Kakkad", "email": "ashishhkakkad@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c201", "name": "Johnny Depp", "email": "johnny_depp@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } } ] |
Create JSON Array Object :
1 2 3 4 5 6 7 8 9 |
let url=URL(string:"https://ashishkakkad.com/contacts.json") 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"] { } } catch { } |
Parse JSON Array Object :
1 2 3 4 5 6 7 8 9 |
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) |
Complete code snippet with UITableView:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
// // ViewController.swift // SwiftJSONParsingDemo // // Created by Ashish Kakkad on 12/10/16. // Copyright © 2016 Kode. All rights reserved. // import UIKit class ViewController: UIViewController { @IBOutlet weak var tableView: UITableView! var names: [String] = [] var contacts: [String] = [] override func viewDidLoad() { super.viewDidLoad() let url=URL(string:"https://ashishkakkad.com/contacts.json") 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 { } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func tableView(_ tableView: UITableView!, numberOfRowsInSection section: Int) -> Int { return self.names.count; } func tableView(_ tableView: UITableView!, didSelectRowAtIndexPath indexPath: IndexPath!) { print("You selected name : "+names[indexPath.row]) } func tableView(_ tableView: UITableView, cellForRowAtIndexPath indexPath: IndexPath) -> UITableViewCell{ var cell = tableView.dequeueReusableCell(withIdentifier: "cell") if !(cell != nil) { cell = UITableViewCell(style: .subtitle, reuseIdentifier: "cell") } cell?.textLabel?.text=self.names[indexPath.row] cell?.detailTextLabel?.text = self.contacts[indexPath.row] return cell! } } |
Posted a gist on github.
Helping, Learning, Coding 🙂