


This will give a one line JSON response containing all the relevant info. We are going to use the Search API method for performing ad-hoc searches.įor example, for searching Twitter about JavaCodeGeeks and retrieving the results in JSON format, here is the corresponding URL: Check the Twitter API Documentation for more info. To illustrate how to use Gson for JSON parsing we are going to parse a JSON response from the Twitter API.

Gson considers both of these as very important design goals.Īdd the Gson JAR to your project’s classpath. Most also do not fully support the use of Java Generics. However, most of them require that you place Java annotations in your classes something that you can not do if you do not have access to the source-code. There are a few open-source projects that can convert Java objects to JSON. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of. It can also be used to convert a JSON string to an equivalent Java object. Gson is a Java library that can be used to convert Java Objects into their JSON representation. For this reason, in this tutorial, I am going to show you how to perform automatic JSON parsing.For this purpose we are going to use the Google Gson library. However, using those classes, a developer has to deal with low level JSON parsing, which in my opinion is tedious and boring. Android includes support for JSON in its SDK as someone can find in the JSON package summary. Additionally, JSON can easily be combined with REST producing clear and easy to use APIs. Its simplicity has helped to gain quite the adoption in favor of the more verbose XML. Apart from XML, JSON is a very common format used in API responses.
