Skip to content

Instantly share code, notes, and snippets.

@fbukevin
Created December 28, 2016 16:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fbukevin/4e61afee9dd55aae09383eb2198a9e20 to your computer and use it in GitHub Desktop.
Save fbukevin/4e61afee9dd55aae09383eb2198a9e20 to your computer and use it in GitHub Desktop.
Load JSON file in Swift 3
func loadJSON(file: String, handler: (NSDictionary) -> Void){
if let path = Bundle.main.path(forResource: file, ofType: "json") {
do {
let jsonData = try NSData(contentsOfFile: path, options: NSData.ReadingOptions.mappedIfSafe)
do {
let json: NSDictionary = try JSONSerialization.jsonObject(with: jsonData as Data
, options: JSONSerialization.ReadingOptions.mutableContainers) as! NSDictionary
handler(json)
} catch {
print("Parse json failed")
}
} catch {
print("Load json failed")
}
} else {
print("\(file).json open failed")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment