There are a couple of ways to work with json on Android. You can use the standard Java/Android libraries, Google’s GSON library, or FasterXML’s Jackson library. There are discussions online which is the best to use and the generally accepted answer is Jackson. The native libraries are slow and cumbersome. GSON is the next best choice. It is really simple to use and a lot faster than the native libs. Jackson is (usally) the fastest one. But Jackson can be a bit confusing to setup.
(Edit - Nov 2015 - Turns out that on Android GSON is faster for small Json strings. This is because Jackson has a longer initialization time. With larger Json strings the longer initialization time is made up by the faster proccessing time. But for shorter Json strings GSON is faster because it initializes faster.)
####First, an overview of how Jackson works.
In your project you need to create a model of the object which the api is sending you. Json is merely a textual representation of some object. Once you have a Pojo (plain old Java object) representing the Json object you can use that with Jackson. You have to give Jackson both the Json (either as a String or a Stream) and the class that you want to build. Jackson will then create an object of that class type using the json to fill its fields.
####Great, lets do it!
Step one is to add the Jackson library to your app’s gradle file.
(replace 2.5.3 with the latest version)
After you’ve added these lines Android Studio should prompt you to ‘sync gradle’. Do that. If that prompt doesn’t come up then you should run Build > Clean.
Awesome, now you have Jackson added to your project.
Next lets create a Pojo for the json that we’ll be using. For this example I will be using this dictionary api from Mashape. This is an example of the Json it returns for a search query ‘book’.
We can see there are two Json objects here. The first is the Response object and that contains and array of Definition objects.
Building the Java models
We need Java models for these objects. To do that we head over to one of my favorite websites, JsonSchema2Pojo. Copy the Json into the input box, set the ‘Source Type’ to Json, and hit ‘Generate’.
Boom. We now have our Java Pojo models. Add them to your project (I like to create a models package and put all my model objects there.)
Create the actual Java objects from the Json
In your project write code to get the Json. You can do this with HttpURLConnection, OkHttp, or just loading a String. Once you have the Json Stream or Json String these are the lines that do the magic:
We are creating a Jackson ObjectMapper and providing it with our input (in this case I used a Stream but you can replace that with a String) and the class that we want to build. Jackson handles everything else.
That’s it. Now you have a Pojo for the server’s response. You can use it in your Java code wherever needed.