Advertisement
Tech

Using GSON Google Library in Android to Treat JSON Strings

This is the second part of the article series on Android code to connect your app to the Internet and retrieve data. In this article we will use libraries to treat JSON strings we receive from the server.

By Jbeerdev
Desk Tech
Reading time 3 min read
Word count 578
Google android Mobile Android sdk
Using GSON Google Library in Android to Treat JSON Strings
Advertisement
Quick Take

This is the second part of the article series on Android code to connect your app to the Internet and retrieve data. In this article we will use libraries to treat JSON strings we receive from the server.

On this page

JSON Library - GSON

We can treat a JSON string in many ways, we can use the JSON objects that Android provides us, but I think it’s interesting enough to create an article for the topic itself, so I’m going to discuss a simple and quick way to treat JSON, and it is using the GSON library. According to the GSON library web page ,

“Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.”

Advertisement

So it’s perfect! That’s all we need. Let’s download it and put the .jar in our project. For this example I have downloaded the 1.7.1 version of the library.

If you want to add an external .jar to your Android code, follow the next steps:

Advertisement
  • Right click on the Android Project.
  • Select “Properties”.
  • Select “Java Build Path” in the left box.
  • Click on the “Libraries” tab in the right box.
  • Click on the button “Add external JARS…”
  • Search for the .jar file you have just downloaded and select it.

Now you have the GSON library in your project, ready to be used.

If we are going to convert a JSON string to a Java object, we need to create a class to hold the information, the class created needs to have the same number and name of arguments as the JSON Object. Let’s see the example.

Advertisement

Our JSON string is going to be as follows:

{“id”:“20”,“text”:" Hi!BrightHub",“date”:“2011-05-14 23:02:54”}, {“id”:“21”, “text”:" Hi!BrightHub Again" , “date”:" 2011-05-15 23:02:54"}, ….

Advertisement

The attributes names in our class have to be:

id, text and date.

Advertisement

So we create a class with these attributes, the getters and setter methods, if you want. I will call this class “JsonClass”.

To convert the JSON stream into a list of objects we have to do the following:

Advertisement

Type collectionType = new TypeToken<List>(){}.getType();

List<JsonClass> jsonList = new Gson().fromJson(response, collectionType);

Advertisement

That’s all. With this piece of code, we get the response from the server and convert it to a list of objects.

We need to tell the GSON library what type of objects are going to be used to hold the JSON, we use the TypeToken for that. And then with the Gson().fromJson() method we make the conversion.

Advertisement

In the source code provided at the end of the article the class I use is “Alertas” and the JSON has the following structure:

{“alertid”:“36”,“alerttext”:“test post”,“alertdate”:“2010-08-20 03:24:42”}

Advertisement

You can check in the repository, it’s the same as I have done above.

Great, so we retrieved the information from the server and we convert the JSON string into a list of known objects… all this will take some time to be accomplished… What do we do? Do we place all the processes in the main UI thread? Do we create a background process for it? In the next article we will talk about this.

Advertisement

References

This post is part of the series: Android Code to Access the Web

With this series of articles I will show Android developers a code example to connect your Android device to the Internet, retrieve data from it and treat that information. Using the right background process in Android is explained too.

  1. Android App Development: How to Access the Internet and Retrieve Info with Your App
  2. Android Development: GSON library and JSON Strings
  3. Android Development: Background Process for a Heavy Task
Keep Exploring

More from Tech

Filed under
Google android Mobile
More topics
Android sdk
Advertisement