2016-02-25 71 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