I've seen a number of similar questions, but none seem to match my use case.
I have a json file structured as follows:
{
"Trains": [{
"Car": "8",
"Destination": "Glenmont",
"DestinationCode": "B11",
"DestinationName": "Glenmont",
"Group": "1",
"Line": "RD",
"LocationCode": "A06",
"LocationName": "Van Ness-UDC",
"Min": "3"
}, {
"Car": "6",
"Destination": "Shady Gr",
"DestinationCode": "A15",
"DestinationName": "Shady Grove",
"Group": "2",
"Line": "RD",
"LocationCode": "A06",
"LocationName": "Van Ness-UDC",
"Min": "3"
}]
}
I'm trying to get the dictionaries for each train. I've tried this (amongst other efforts), but I cant get my head around it. Here is my code:
jsonArray = [try! JSONSerialization.jsonObject(with: data!, options: .mutableContainers)] as! [String]
for train in jsonArray {
print(train["name"])
}
This doesn't compile.
My jsonArray is set up as:
var jsonArray = [Any]()
{}is dictionary,[]is array. Therefore the root object is a dictionary ([String:Any]) and the value for keyTrainsis an array ([[String:Any]]), not[Any]. There is no[String]type at all in the JSON.