#Swift2.0のソースコード
ViewController.swift
import UIKit
class ViewController: UIViewController {
private var json:NSDictionary!
override func viewDidLoad() {
super.viewDidLoad()
getJson()
}
func getJson() {
let URL:NSURL = NSURL(string: "http://express.heartrails.com/api/json?method=getStations&name=%E6%96%B0%E5%AE%BF")!
let jsonData :NSData = NSData(contentsOfURL: URL)!
do {
json = try NSJSONSerialization.JSONObjectWithData(jsonData, options: .MutableContainers) as! NSDictionary
} catch {
// エラー処理
}
let response:NSDictionary = json.objectForKey("response") as! NSDictionary
let station:NSArray = response.objectForKey("station") as! NSArray
for var i=0 ; i < station.count ; i++ {
print(station[i].objectForKey("prefecture") as! NSString)
}
}
}
赤枠の部分をinfo.plistに追加する。