I am really struggling with trying to read a JSON file in Swift so that I can play around with it. I spent most of 2 days searching again and trying different methods, but with no luck, so I signed up for StackOverFlow to find out if anyone could point me in the right direction .....
My JSON file is called test.json and contains the following:
{ "person":[ { "name": "Bob", "age": "16", "employed": "No" }, { "name": "Vinny", "age": "56", "employed": "Yes" } ] }
The file is stored in the documents directly, and I access it using the following code:
let file = "test.json" let dirs : String[] = NSSearchPathForDirectoriesInDomains( NSSearchpathDirectory.DocumentDirectory, NSSearchPathDomainMask.AllDomainMask, true) as String[] if (dirs != nil) { let directories: String[] = dirs let dir = directories[0] let path = dir.stringByAppendingPathComponent(file) } var jsonData = NSData(contentsOfFile:path, options: nil, error: nil) println("jsonData \(jsonData)"
If someone can just give me a push in the right direction, how can I de-serialize a JSON file and put it in an accessible Swift object, I will be infinitely grateful!
Respectfully,
Krivvenz.
json xcode ios8 swift nsjsonserialization
Krivvenz Jun 25 '14 at 2:04 a.m. 2014-06-25 14:04
source share