Swift 4 and Foundation has finally answered the question of how to parse JSON with Swift.
There has been a number of great libraries for this, but it is quite refreshing to see a fully-supported solution that is easy to adopt but also provides the customization you need to encode and decode complex scenarios.
It’s worth noting that everything discussed here applies to any
Decoder implementation, including
PropertyListEncoder, for instance. You can also create a custom implementations of these if you need something different like XML. The rest of this blog post will focus on JSON parsing because that is the most relevant to most iOS developers.
If your JSON structure and objects have similar structure, then your work is really easy.
Here’s an example JSON document for a beer:
1 2 3 4 5 6