Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import UIKit
- class ViewController: UIViewController {
- override func viewDidLoad() {
- super.viewDidLoad()
- // load file json
- let urlString = "http://www.learnswiftonline.com/Samples/subway.json"
- let url : NSURL = NSURL(string: urlString)!
- let urlSession = NSURLSession.sharedSession()
- // query url
- let jsonQuery = urlSession.dataTaskWithURL(url) { (data, reponse, error) in
- if (error != nil){
- print("\(error!.localizedDescription)");
- }
- var err : NSError?
- var jsonResult = (try! NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers)) as! NSDictionary
- if (err != nil) {
- print("error : \(err?.localizedDescription)")
- }
- let jsonData : NSArray! = jsonResult["stations"] as! NSArray
- if jsonData != nil {
- print("\(jsonData)");
- }
- for var element in jsonData {
- print((element["stationName"]))
- print((element["buildYear"]))
- }
- }
- jsonQuery.resume()
- }
- override func didReceiveMemoryWarning() {
- super.didReceiveMemoryWarning()
- // Dispose of any resources that can be recreated.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement