Parsing a JSON response
Now that we have successfully retrieved a JSON response from an API, it is time to learn how to use the data we have obtained. To do so, we need to parse the JSON payload. This is because the payload is a plain string representing the data object, and we are interested in the specific properties of that object. If you look closely at Figure 5.2, you may notice that the JSON contains breed information, an image URL, and some other bits of information. However, for our code to use that information, first, we must extract it.
As mentioned in the introduction, multiple libraries exist that will parse a JSON payload for us. The most popular ones are Google’s GSON (see https://github.com/google/gson) and, more recently, Square’s Moshi (see https://github.com/square/moshi). Moshi is very lightweight, which is why we have chosen to use it in this chapter.
What do JSON libraries do? Basically, they help us convert data classes into JSON strings ...