JSON Data
{ "contacts": [ { "id": "c200", "name": "Ravi Tamada", "email": "ravi@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" } }, { "id": "c202", "name": "Leonardo Dicaprio", "email": "leonardo_dicaprio@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c203", "name": "John Wayne", "email": "john_wayne@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c204", "name": "Angelina Jolie", "email": "angelina_jolie@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "female", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c205", "name": "Dido", "email": "dido@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "female", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c206", "name": "Adele", "email": "adele@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "female", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c207", "name": "Hugh Jackman", "email": "hugh_jackman@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c208", "name": "Will Smith", "email": "will_smith@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c209", "name": "Clint Eastwood", "email": "clint_eastwood@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c2010", "name": "Barack Obama", "email": "barack_obama@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c2011", "name": "Kate Winslet", "email": "kate_winslet@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "female", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } }, { "id": "c2012", "name": "Eminem", "email": "eminem@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone": { "mobile": "+91 0000000000", "home": "00 000000", "office": "00 000000" } } ] }
ViewController.swift
//
// ViewController.swift
// JsonApp1
//
// Created by Divakar Singh on 4/4/17.
// Copyright © 2017 Divakar Singh. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "http://api.androidhive.info/contacts/")
let task = URLSession.shared.dataTask(with: url!){
(data, response, error) in
if error != nil{
print("Error")
}else{
if let content = data
{
do{
let myJson = try JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers) as AnyObject
// let print all contact data
if let contact = myJson["contacts"] as? NSArray
{
// To print single data
/*let info = contact[0] as? NSDictionary
print(info?["id"] as! String)
print(info?["name"] as! String)
print(info?["email"] as! String)
print(info?["address"] as! String)
print(info?["phone"])*/
// To print array data
for i in 0..<contact.count
{
let info = contact[i] as? NSDictionary
print(info?["id"] as! String)
print(info?["name"] as! String)
print(info?["email"] as! String)
print(info?["address"] as! String)
print(info?["phone"])
print("\n**********************NEXT************************\n")
}
}
}catch
{
}
}
}
}
task.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
new
No comments:
Post a Comment