jarv81178
jarv81178

Reputation: 83

how to get data from JSON array SwiftyJSON?

I would like to be able to get some data from a JSON array: here so below, I am trying to show EARLIEST > DATE

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)")
}

Upvotes: 0

Views: 301

Answers (1)

vadian
vadian

Reputation: 285064

The date of earliest is in a dictionary timeAdjustments in a dictionary searchCriteria

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

Upvotes: 1

Related Questions