Advertisement
zeev

Jason Reader

Apr 24th, 2017
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 2.43 KB | None | 0 0
  1. //
  2. //  ViewController.swift
  3. //  MyJson
  4. //
  5. //  Created by HackerU on 24/04/2017.
  6. //  Copyright © 2017 HackerU. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ViewController: UIViewController {
  12.     let s=URLSession.shared
  13.    
  14.     override func viewDidLoad() {
  15.         // json1()
  16.         //json2()
  17.         json3() //for Elinor
  18.     }
  19.  
  20.     //example 1 - simple read
  21.     func json1()
  22.     {
  23.         let url=URL(string: "http://nikita.hackeruweb.co.il/hackSwift/mini.json")!
  24.         s.dataTask(with:url, completionHandler:{(d,r,e)in
  25.             //get the information as raw data into json variable
  26.             let json = try! JSONSerialization.jsonObject(with: d!, options: .mutableContainers) as! [String:Any];
  27.             print (json)
  28.         }).resume()
  29.     }
  30.    
  31.     //example 2 - parse the data
  32.     func json2()
  33.     {
  34.         let url=URL(string: "http://nikita.hackeruweb.co.il/hackSwift/mini.json")!
  35.         s.dataTask(with: url, completionHandler: {(d,r,e)in
  36.             //get the raw information from our json data
  37.             let json = try! JSONSerialization.jsonObject(with: d!, options: .mutableContainers) as! [String:Any];
  38.            
  39.             print("\(json["name"]!) is \(json["age"]!) years old")
  40.             print("and he knows the following languages:")
  41.             for singleLang in json["langs"] as! [String] //print all the languages
  42.             {
  43.                 print(singleLang)
  44.             }
  45.            
  46.         }).resume()
  47.     }
  48.    
  49.     //example 3 - complexed json
  50.     func json3()
  51.     {
  52.         let url=URL(string: "http://nikita.hackeruweb.co.il/hackDroid/items.json")!
  53.         s.dataTask(with: url, completionHandler: {(d,r,e)in
  54.             if let json = try? JSONSerialization.jsonObject(with: d!, options: .mutableContainers) as! [String:Any]
  55.             {
  56.                 let hits = json["hits"] as! [Any] //get array hits from json
  57.                 for hitSingle in hits {//iterate over all "hits"
  58.                     let source = (hitSingle as! [String:Any])["_source"]! //get "_source" from each hit
  59.                     let info=(source as! [String:Any])["info"]!
  60.                     if let icon=(info as! [String:Any])["icon"]
  61.                     {
  62.                         print (icon)
  63.                     }
  64.                     else
  65.                     {
  66.                         print ("No bladdy icon")
  67.                     }
  68.                 }
  69.             }
  70.         }).resume()
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement