0

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

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, changed date to time now it says the same for each whereas should be a different time, when I add it inside the for loop, it's the same?! NSJSONSerialization.JSONObjectWithData(jsonData!, options: []) as! [String:AnyObject] let object = JSON(readableJSON) let searchCriteria = object["journeys"] let earliest = object["searchCriteria"]["timeAdjustments"]["earliest"]["time"].stringValue for option in searchCriteria.arrayValue { let arrivalDateTime = option["arrivalDateTime"].stringValue arrivalArray.append(arrivalDateTime) earliestArray.append(earliest) }
There is only one earliest key in the entire text and you get only a different arrivalDateTime in the repeat loop. Maybe you mean startDateTime

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.