Reputation: 378
I'm using Factual API to obtain information about businesses. For the place hours, I get back a single string:
"{\"monday\":[[\"11:00\",\"23:59\"]],\"friday\":[[\"11:00\",\"23:59\"]],\"sunday\":[[\"00:00\",\"1:00\"],[\"10:00\",\"23:59\"]],\"tuesday\":[[\"11:00\",\"23:59\"]],\"thursday\":[[\"11:00\",\"23:59\"]],\"saturday\":[[\"00:00\",\"1:00\"],[\"10:00\",\"23:59\"]],\"wednesday\":[[\"11:00\",\"23:59\"]]}"
I've tried several things to break this up and I'm banging my head. Any ideas how I can parse this string to be able to get open/close times by day to determine if a place is currently open? Thanks a lot in advance!!
Upvotes: 1
Views: 61
Reputation: 66302
You can convert the string to NSData
, then to a Dictionary
:
let string = "{\"monday\":[[\"11:00\",\"23:59\"]],\"friday\":[[\"11:00\",\"23:59\"]],\"sunday\":[[\"00:00\",\"1:00\"],[\"10:00\",\"23:59\"]],\"tuesday\":[[\"11:00\",\"23:59\"]],\"thursday\":[[\"11:00\",\"23:59\"]],\"saturday\":[[\"00:00\",\"1:00\"],[\"10:00\",\"23:59\"]],\"wednesday\":[[\"11:00\",\"23:59\"]]}"
let data = string.dataUsingEncoding(NSUTF8StringEncoding)!
let dictionary = try! NSJSONSerialization.JSONObjectWithData(data, options: []) as! [String : AnyObject]
print(dictionary["monday"]![0][0]) //11:00
print(dictionary["monday"]![0][1]) //23:59
Of course, it would be a bit easier to get it as a dictionary in the first place.
Upvotes: 1