Create a gist now

Instantly share code, notes, and snippets.

Embed
JSON Array Parsing in Swift Langauage - Swift 3 – iOS 10 – Xcode 8 GM
//
// 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:"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 {
}
}
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!
}
}
@polamgh

This comment has been minimized.

Show comment
Hide comment
@polamgh

polamgh Nov 2, 2016

in xcode 8
in simulator is ok but when select the phone got this message 'Ambiguous use of subscript' at the line 'let aObject = arrJSON[index] as! [String : AnyObject]'

polamgh commented Nov 2, 2016

in xcode 8
in simulator is ok but when select the phone got this message 'Ambiguous use of subscript' at the line 'let aObject = arrJSON[index] as! [String : AnyObject]'

@Xheno

This comment has been minimized.

Show comment
Hide comment
@Xheno

Xheno Dec 20, 2016

I have looked for this snippet all over the internet. Thank you

Xheno commented Dec 20, 2016

I have looked for this snippet all over the internet. Thank you

@Dengekiko

This comment has been minimized.

Show comment
Hide comment
@Dengekiko

Dengekiko Feb 7, 2017

@polamgh did you ever figure it out?

@polamgh did you ever figure it out?

@chaoticbit

This comment has been minimized.

Show comment
Hide comment
@chaoticbit

chaoticbit Feb 10, 2017

@polamgh I encountered with the same issue. Xcode 8 swift 3. Did you figure it out?

@polamgh I encountered with the same issue. Xcode 8 swift 3. Did you figure it out?

@macksz

This comment has been minimized.

Show comment
Hide comment
@macksz

macksz Mar 1, 2017

Hi,
Please, I need help.
I encountered with the same issue too. Xcode 8 swift 3. Did you figure it out?
Kind Regards

macksz commented Mar 1, 2017

Hi,
Please, I need help.
I encountered with the same issue too. Xcode 8 swift 3. Did you figure it out?
Kind Regards

@maninipoornam

This comment has been minimized.

Show comment
Hide comment
@maninipoornam

maninipoornam Mar 2, 2017

@polamgh @Dengekiko Did anyone figure that out. Having same issue.

@polamgh @Dengekiko Did anyone figure that out. Having same issue.

@mayuritalaviya

This comment has been minimized.

Show comment
Hide comment
@mayuritalaviya

mayuritalaviya Mar 22, 2017

@maninipoornam : Just small changes in above code and it's working on phone.

let allContacts = try JSONSerialization.jsonObject(with: allContactsData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String : NSArray]
            if let arrJSON = allContacts["contacts"] {
                for index in 0...arrJSON.count-1 {
                    
                    let aObject = arrJSON[index] as! [String: Any]
                    
                    names.append(aObject["name"] as! String)
                    contacts.append(aObject["email"] as! String)
                }
            }

@maninipoornam : Just small changes in above code and it's working on phone.

let allContacts = try JSONSerialization.jsonObject(with: allContactsData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String : NSArray]
            if let arrJSON = allContacts["contacts"] {
                for index in 0...arrJSON.count-1 {
                    
                    let aObject = arrJSON[index] as! [String: Any]
                    
                    names.append(aObject["name"] as! String)
                    contacts.append(aObject["email"] as! String)
                }
            }
@txdywy

This comment has been minimized.

Show comment
Hide comment
@txdywy

txdywy Apr 25, 2017

let allContactsData = try Data(contentsOf: self.url!)
let allContacts = try JSONSerialization.jsonObject(with: allContactsData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String : AnyObject]
if let arrJSON = allContacts["data"] as! [String]?{
for index in 0...arrJSON.count-1 {

                let aObject = arrJSON[index] as! String

txdywy commented Apr 25, 2017

let allContactsData = try Data(contentsOf: self.url!)
let allContacts = try JSONSerialization.jsonObject(with: allContactsData, options: JSONSerialization.ReadingOptions.allowFragments) as! [String : AnyObject]
if let arrJSON = allContacts["data"] as! [String]?{
for index in 0...arrJSON.count-1 {

                let aObject = arrJSON[index] as! String
@robertoltrocha

This comment has been minimized.

Show comment
Hide comment
@robertoltrocha

robertoltrocha Aug 28, 2017

The code is running well but when I lost the internet connection the code crash in the line "let allContactsData = try Data(contentsOf: url!)". Can someone help me? I am using swift 3

The code is running well but when I lost the internet connection the code crash in the line "let allContactsData = try Data(contentsOf: url!)". Can someone help me? I am using swift 3

@X901

This comment has been minimized.

Show comment
Hide comment
@X901

X901 Sep 12, 2017

Thank You

X901 commented Sep 12, 2017

Thank You

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment