Review the basics of parsing JSON data into simple Swift objects for use by the iOS app.
- [Narrator] So now that we're able to download data into our application, we need to transform that data into useful data objects. Our product list view controller uses a list of product objects to display data, so we'll need to transform the source data format to product objects. The source format, in this case, is json, so it makes sense now to talk about json parsing and building a simple json parser for our application. To help make our json parsing code a little easier to understand, I'm going to break down this process into two steps. The first step is deserialization.
Deserialization can be thought of as a step where we take the low level ones and zeros returned from our network task and as a data object and transform it into a native swift type. Namely, the Any type. The Any type will let us hold dictionary type data which is one way to think of json objects. So, treating this data as a dictionary type helps make the next process also simple and that is the decoding process. Decoding will take the resulting native swift object and go through the dictionary and read all the keys and copy the values into the correct property fields of our native object.
We'll attempt to make this process a little generic by defining a protocol to which the product type will conform. That way, we can update the objects into being decodable by using some handy features of swift and also as part of our json parsing library, we're going to define our own custom json object type allies, which will simply type allies the string any object dictionary to something that's a little more easy to recognize. Alright, so let's get to the code.
So, here we are back in our product list view controller. Let's go ahead and on the application level go ahead and create a new file and we will call this file JSONParsing and in this file, we're going to start off by creating a simple protocol called JSONDecodable. This protocol is going to have a single method and that is going to be the initialization method, so we're going to with this protocol declare that all types implement this protocol; implement this initializer and the way we're going to do this is introduce a object called JSONDecoder, so way this is going to work is we're going to create a JSONDecoder object outside of the initialization step of this object and pass it in, so we'll have the raw dictionary with a little helper called JSONDecoder and let the initializer extract all the values safely using our wrapped decoding methods and because json is an inherently failable operation meaning that keys could be missing or types could be misaligned such as you're expecting a sting type and the type returned in json is an integer where types are mismatched, then we want the decoding process to fail, so at any point in the decoding process that any of these errors occur, no object will be instantiated, so now the Xcode compiler is complaining that I do not have a type defined as json decoder, so let's go ahead and create that and this is going to be a simple class and the way that we are going to initialize this class; we'll keep it very simple for now.
We'll just have our json object here and this is going to be a dictionary that looks just like our assumed json dictionary type and we're going to initialize this the same way and we will store this, so that we can use this later and that's all we will need to actually create this json decoder for now, but as I mentioned previously, I'm going to assume that this type here on line 17, the string Any dictionary is actually what we are going to name a json object, so let's go ahead and create a type allies and I'm going to say JSONObject is actually equal to string Any and what this lets me do is refer to this as a json object rather than a plain old string Any dictionary.
Now, this helps with our type checking and our compiling, but we can pass in any string, any dictionary to this decoder, but we're just trying to assume and make it straight in our code that what we really mean is that this is a json object that is in the form of a dictionary.
- Requesting data from an HTTP server
- Parsing with JSON
- Decoding JSON
- Parsing data with XML
- Loading data a page at a time to avoid HTTP errors
- Error handling
- Loading and caching image data