Swift version: 5.6
SwiftyJSON is a super-simplified JSON parsing library that gives you clearer syntax than the built-in iOS libraries (yes, even more than JSONEncoder
from Codable
), and is free.
You can download it from here – unzip the file you downloaded, then look in its Source directory and drag SwiftyJSON.swift into your Xcode project. To use SwiftyJSON, you need to convert your JSON string into a Data
object, then send it in for parsing. Once that's done, you simply request data in the format you want, and (here's the awesome bit) SwiftyJSON is guaranteed to return something.
That "something" is going to be your data, if all things are in good shape. But if you requested the wrong thing (either with a typo, or because you didn't understand your JSON structure correctly) or if the JSON has changed, SwiftyJSON will just return a default value instead.
To get you started, here is some example JSON:
let json = "{ \"people\": [{ \"firstName\": \"Paul\", \"lastName\": \"Hudson\", \"isAlive\": true }, { \"firstName\": \"Angela\", \"lastName\": \"Merkel\", \"isAlive\": true }, { \"firstName\": \"George\", \"lastName\": \"Washington\", \"isAlive\": false } ] }"
That contains an array of three people, each of which have a first name, a last name, and an "is alive" status. To parse that using SwiftyJSON and print out all the first names, here's the code:
if let data = json.data(using: .utf8) {
if let json = try? JSON(data: data) {
for item in json["people"].arrayValue {
print(item["firstName"].stringValue)
}
}
}
It's the arrayValue
and stringValue
properties that do all the magic: the first one returns the array of people or an empty array if the "people" element didn't exist, and the second one returns the "firstName" property of a person, or an empty string if it wasn't set. So, no matter what happens, that code will work, which means it's easy to write and safe to run.
Sometimes JSON has quite deeply nested dictionaries, but that's OK: SwiftyJSON can navigate through multiple levels in one call, and if any one level fails you'll still get back your default value. For example, if you have JSON like this:
{
"metadata":{
"responseInfo":{
"status":200,
"developerMessage":"OK",
}
}
}
You might want to check that the status code is 200 before continuing. To do that, just read the "metaData", "responseInfo" and "status" values all at once, and ask SwiftyJSON for its intValue
– you'll either get the correct number (200) or 0 if any of those values don't exist. Like this:
if json["metadata"]["responseInfo"]["status"].intValue == 200 {
// we're OK to parse!
}
SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!
Sponsor Hacking with Swift and reach the world's largest Swift community!
Available from iOS 7.0 – see Hacking with Swift tutorial 7
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.