P.Coder P.Coder - 5 months ago 24
Swift Question

Swift: Parsing JSON string without key

I would like to parse json string as following, I am wondring how to print [times] values

Json String:

{
date: "2014-08-13",
method: "Makkah: Umm al-Qura University, Makkah",
latitude: "30.0599153",
longtude: "31.2620199",
timezone: "+3",
times: [
"04:52",
"06:21",
"13:00",
"16:36",
"19:38",
"19:38",
"21:08"
] }


This is my code:

let json = try NSJSONSerialization.JSONObjectWithData(data!, options:.AllowFragments)

let todayDate = json["date"]
let method = json["method"]
let latitude = json["latitude"]
let longtude = json["longtude"]
let timezone = json["timezone"]


so the question is, How to print the times, Thanks

Answer
let times = json["times"] as! [String]

for time in times {
   print(time) 
}

or in one line

times.forEach{ print($0) }
Comments