Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read JSON file with Swift 3

Tags:

json

swift3

I have a JSON file called points.json, and a read function like:

private func readJson() {     let file = Bundle.main.path(forResource: "points", ofType: "json")     let data = try? Data(contentsOf: URL(fileURLWithPath: file!))     let jsonData = try? JSONSerialization.jsonObject(with: data!, options: []) as! [String:Any]     print(jsonData) } 

It does not work, any help?

like image 802
Xie Avatar asked Nov 05 '16 13:11

Xie


People also ask

How do I open a JSON file in Swift?

if let path = Bundle. main. path(forResource: "assets/test", ofType: "json") { do { let data = try Data(contentsOf: URL(fileURLWithPath: path), options: . alwaysMapped) let jsonObj = try JSON(data: data) print("jsonData:\(jsonObj)") } catch let error { print("parse error: \(error.

What is Jsonserialization in Swift?

An object that converts between JSON and the equivalent Foundation objects.

What is JSON parsing in Swift?

Swift JSON ParsingJSON stands for JavaScript Object Notation. It's a popular text-based data format used everywhere for representing structured data. Almost every programming language supports it with Swift being no exception. You are going to use JSON a lot throughout your career, so make sure you don't miss out.


2 Answers

Your problem here is that you force unwrap the values and in case of an error you can't know where it comes from.

Instead, you should handle errors and safely unwrap your optionals.

And as @vadian rightly notes in his comment, you should use Bundle.main.url.

private func readJson() {     do {         if let file = Bundle.main.url(forResource: "points", withExtension: "json") {             let data = try Data(contentsOf: file)             let json = try JSONSerialization.jsonObject(with: data, options: [])             if let object = json as? [String: Any] {                 // json is a dictionary                 print(object)             } else if let object = json as? [Any] {                 // json is an array                 print(object)             } else {                 print("JSON is invalid")             }         } else {             print("no file")         }     } catch {         print(error.localizedDescription)     } } 

When coding in Swift, usually, ! is a code smell. Of course there's exceptions (IBOutlets and others) but try to not use force unwrapping with ! yourself and always unwrap safely instead.

like image 134
Eric Aya Avatar answered Oct 04 '22 15:10

Eric Aya


The Swift 5 / iOS 12.3 code below shows a possible rewrite of your method that avoids force unwrap on optional values and handles gently potential errors:

import Foundation  func readJson() {     // Get url for file     guard let fileUrl = Bundle.main.url(forResource: "Data", withExtension: "json") else {         print("File could not be located at the given url")         return     }      do {         // Get data from file         let data = try Data(contentsOf: fileUrl)          // Decode data to a Dictionary<String, Any> object         guard let dictionary = try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] else {             print("Could not cast JSON content as a Dictionary<String, Any>")             return         }          // Print result         print(dictionary)     } catch {         // Print error if something went wrong         print("Error: \(error)")     } } 
like image 24
Imanou Petit Avatar answered Oct 04 '22 14:10

Imanou Petit