Read & Parse local JSON file in Swift
ReadJsonFile
Read & Parse local JSON file in Swift
Usage
- Build model for your json data.
{
"firstname": "Emrah",
"lastname": "Turan",
"website": "www.emrahturan.com.tr"
}
struct DataModel: Codable {
var firstname: String
var lastname: String
var website: String
}
- Create a function to call your json file. Give json filename and model as parameter.
private func getData(completion: @escaping(DataModel?) -> Void) {
readFromFile(fileName: "data", completion: completion)
}
- Data read from json file is ready to be used.
getData { data in
guard let data = data else { return }
print("firstname: ", data.firstname)
print("lastname: ", data.lastname)
print("website: ", data.website)
}
Feedback
If you have feedback about my ReadJsonFile Project do not forget to reach me via email or Discussions section of this repo.