2016-02-25 47 views
0

我希望能夠從一個JSON陣列得到一些數據:here 所以下面,我試圖表明EARLIEST>日期如何從JSON數組中獲取數據SwiftyJSON?

func parseJSONtowns() { 
    let url = NSURL(string: "https://api.tfl.gov.uk/journey/journeyresults/\(selectedStation)/to/\(selectedStation1)") 
    let jsonData = NSData(contentsOfURL: url!) 
    let readableJSON = try! NSJSONSerialization.JSONObjectWithData(jsonData!, options: []) as! [String:AnyObject] 
    let object = JSON(readableJSON) 
    let searchCriteria = object["earliest"] 
    for option in searchCriteria.arrayValue { 
     let commonName = option["date"].stringValue 
     commonNameArray.append(commonName) 
    } 
    selectedStation = selectedStation.stringByReplacingOccurrencesOfString("%20", withString: " ") 
    NumberOfRows = commonNameArray.count 
    NSLog("\(url)") 
    NSLog("number of rows: \(NumberOfRows)") 
} 

回答

1

earliestdate是在字典timeAdjustments在字典searchCriteria

let object = JSON(readableJSON) 
let earliest = object["searchCriteria"]["timeAdjustments"]["earliest"]["date"].stringValue 
print(earliest) 
+0

謝謝,改變日期,現在它說每個相同,而應該是一個不同的時間,當我將它添加到for循環,它是一樣的? 'NSJSONSerialization.JSONObjectWithData(jsonData !, options:[])as! [String:AnyObject] let object = JSON(readableJSON) let searchCriteria = object [「journeys」] let earliest = object [「searchCriteria」] [「timeAdjustments」] [「early」] [「time」]。stringValue 用於searchCriteria.arrayValue選項{ 讓arrivalDateTime =選項[ 「arrivalDateTime」]。stringValue的 arrivalArray.append(arrivalDateTime) earliestArray.append(最早) }' – jarv81178

+0

只有一個'在整個文本earliest'鍵並且你在重複循環中只得到一個不同的'arrivalDateTime'。也許你的意思是'startDateTime' – vadian