1

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!!

3
  • What have your tried so far? I'd try some sort of regex. Commented Dec 29, 2015 at 22:42
  • The string is valid JSON. Can you show how you're getting it? You should probably be putting the data in a dictionary instead of a string. Commented Dec 29, 2015 at 22:44
  • So the data is returned to me in a string, it would be easier with a dictionary for sure. I've tried componentsSeperatedByString to get little pieces, but it just is hard to crack this nut. What kind of regex would help me out? Thanks a lot for your responses! Commented Dec 29, 2015 at 22:46

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

This is great, exactly what I was looking for. Thanks for the help!

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.