1

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

0

1 Answer 1

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

for time in times {
   print(time) 
}

or in one line

times.forEach{ print($0) }
Sign up to request clarification or add additional context in comments.

1 Comment

To clarify, times is an array. The JSON you posted is a dictionary containing a variety of key/value pairs. One of those is the key times, and the value for that key is an array of time strings.

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.