How to parse a JSON file in swift?
This answer was last revised for Swift 5.3 and iOS 14.4 SDK.
Given some already obtained JSON data, you can use JSONDecoder
to decode it into your Decodable
model (or a collection of models).
let data: Data = /* obtain your JSON data */
let model = try JSONDecoder().decode(Model.self, from: data)
Such model must conform to the Decodable
protocol and contain correct mapping between properties and JSON dictionary keys. As an example, consider the following JSON array containing search results of cities beginning with "Wa".
[
{
"id": 123,
"city": "Washington",
"region": "D.C.",
"country": "United States"
},
{
"id": 456,
"city": "Warsaw",
"region": "Mazowieckie",
"country": "Poland"
},
...
]
For that, you need to create a model that contains the correct properties of correct types. If you're using a web API, its documentation will be of great help here.
struct SearchResult: Decodable {
let id: Int
let city: String
let region: String
let country: String
}
Then decode the data with JSONDecoder
:
let results = try JSONDecoder().decode([SearchResult].self, from: data)
Given a new array of decoded search results, call one of UITableView
's functions to reload its data. Note that the decode
function can throw an error which you must somehow handle.
To learn more about decoding custom types in Swift and more advanced usage of the Codable
APIs, I recommend checking out this documentation article.
Making the API Request
var request: NSURLRequest = NSURLRequest(URL: url)
var connection: NSURLConnection = NSURLConnection(request: request, delegate: self, startImmediately: false)
Preparing for the response
Declare an array as below
var data: NSMutableData = NSMutableData()
Receiving the response
1.
func connection(didReceiveResponse: NSURLConnection!, didReceiveResponse response: NSURLResponse!) {
// Received a new request, clear out the data object
self.data = NSMutableData()
}
2.
func connection(connection: NSURLConnection!, didReceiveData data: NSData!) {
// Append the received chunk of data to our data object
self.data.appendData(data)
}
3.
func connectionDidFinishLoading(connection: NSURLConnection!) {
// Request complete, self.data should now hold the resulting info
// Convert the retrieved data in to an object through JSON deserialization
var err: NSError
var jsonResult: NSDictionary = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions.MutableContainers, error: nil) as NSDictionary
if jsonResult.count>0 && jsonResult["results"].count>0 {
var results: NSArray = jsonResult["results"] as NSArray
self.tableData = results
self.appsTableView.reloadData()
}
}
When NSURLConnection
receives a response, we can expect the didReceiveResponse
method to be called on our behalf. At this point we simply reset our data by saying self.data = NSMutableData()
, creating a new empty data object.
After a connection is made, we will start receiving data in the method didReceiveData
. The data argument being passed in here is where all our juicy information comes from. We need to hold on to each chunk that comes in, so we append it to the self.data object we cleared out earlier.
Finally, when the connection is done and all data has been received, connectionDidFinishLoading
is called and we’re ready to use the data in our app. Hooray!
The connectionDidFinishLoading
method here uses the NSJSONSerialization
class to convert our raw data in to useful Dictionary
objects by deserializing the results from your Url.