Skip to content

JSON Parsing and how to use GSON in Android

JSON parsing

In this article, you will learn how to do JSON parsing in your android application, both doing the traditional way, as well as using 3rd party libraries.

By the way! If you are interested in becoming a professional software developer, make sure to check out our courses!

The sharing of data over the Internet is a prevalent task. Nowadays, almost every application has some features to exchange the data with the server over the Internet.

The details send and receive from the server can be in XML or JSON format. When you want to use the data store using these file formats in the database, you must apply some parsing of data in your Android Application. For the JSON file, you have to use the JSON parsing, and for XML files, you can use the XML parsing. In this article, you will learn how to parse the JSON format data and use it in our Android Application. So, let’s get started with JSON parsing.

What is JSON?

JSON stands for JavaScript Object Notation. The JSON is used to exchange the data from the server to the desired place over the Internet. The XML parsing is more complicated as compared to JSON parsing.

JSON is structured, lightweight, and an independent data exchange format used to parse data. JSON is also language-independent. JSON uses the syntax of JavaScript, which is limited to text. Apart from this, nowadays, most of the APIs available to us is sending data in the form of JSON and receiving that data from API, you have to do JSON parsing, and it is straightforward to do JSON parsing.

To manipulate JSON data and to play with JSON, Android provides four classes. These classes are:

  • JSONObject
  • JSONArray
  • JSONStringer
  • JSONTokenizer

The form you have selected does not exist.

JSON Structure

JSON uses two types of brackets which are as below:

  • [] – JSON uses square brackets to declare an Array’s elements in JSON.
  • {} – JSON uses the curly brackets to create JSON objects elements.

JSON has the following types of structures that generally used, which are as below:

JSON Array

The elements present the list of values in the ([…]), known as Arrays. Inside the JSON array, you can put values like Integer, Boolean, String, Float, and also you can put some other JSON array or JSON object in a particular JSON array. 

JSON Objects

The elements present inside the curly ({…}) brackets are known as Objects. The JSON object contains key/value pairs like the Hash map. In the JSON object, we can also use nested JSONObjects, which are very commonly used nowadays. 

JSON Key-Value

The values can be in any primitive data type and are the value corresponding to a particular key. For example: “Username”:”Denis Panjuta”. Here the Username is key and the value corresponding is “Denis Panjuta”.

The form you have selected does not exist.

JSON parsing

JSON Parsing Functions

This is a list of functions in android which can be used for JSON Parsing:

  • get(int index): It is used to get the value of the object type present in the JSON array. It throws an exception if there is no JSONObject in the JSON array.
  • getType(int index): It gets the value of any of the mentioned types present in the JSON array. The type can be String, Int, Boolean, Long, Double, JSONArray, or JSONObject. 
  • length(): It is used to get the length of the array.
  • opt(int index): It returns the Object Type value in the JSONArray on a particular index.
  • optType(int index ): It returns the value of the mentioned types in the JSONArray on a particular index.

JSON Parsing in Android

You have learned the concept of JSON Parsing and its functions. Now, let’s implement the concept in the android project.

Create a New Project

Create a new Android Studio project:

JSON parsing

Select the option as Start a new Android Studio project:

JSON parsing

Select Empty Activity and click on Next:

Name the project as you want and update the package name. I have named the project as JSON Parsing and also updated the package name as com.jsonparsing. And also, select the project location where you want to save the project.

Create the project using Kotlin.

In this demo project, I will show you how to create the JSON file in the assets folder. Parse the JSON file using the functions that you have learned in the above article and also using the third party library “GSON”.

Now, write JSON in the assets folder. This is how you can do it as Go to > app > Right-click on the app and select the New > Folder > Assets Folder > click on Finish. You will see the Assets Folder beneath the java folder in the structure.

After creating the Assets Folder create a JSON file and write your JSON value in it.

I have the JSON example of the list of users with their details as below:

Now let’s use the JSON file. Right-click on the Assets folder > New > File and Name the file with extension .json as I have named it as Users.json.

Now copy the above JSON value in the Users.json file and Save it.

Now we have the data of Users in the form of JSON. Which includes id, name, and other details of “user” in it. Let’s use it in the example.

The form you have selected does not exist.

Now, I will show you how to parse it and display the list of Users in the RecyclerView.

Add the below dependency to your app-level build.gradle file to use RecyclerView

Add the RecyclerView in the activity_main.xml as below:

Now let’s create the custom item layout for the user to use it in the RecyclerView.

Create a Custom Layout file for RecyclerView

item_user_layout.xml

Create a User Data Model Class

This Data Model class is basically mirroring the data that we have in JSON format. Can you see how the names of our variables are the same as the names of properties in the JSON file? This is important for the process to work.

Create a User Adapter Class

JSON parsing from JSON file and Attach the adapter to the RecyclerView

Now, Get the JSON file’s values and then the user values to the UserAdapter class to put that values on the RecyclerView. Go to the MainActivity.kt file and add the below code:

MainActivity.kt

Now, run the application and see the output.

Output

As you can see all the detail of the JSON object is parsed and displayed in the RecyclerView with the individual user details.

JSON parsing using GSON

Now, you have seen the JSON parsing with the functions and which we need to do it manually line by line.

Here I will show you how to do it with a third-party library. There are many libraries that are used to parse JSON objects with the help of the data model class.

I will show you the third party library GSON which is quite popular. You can check out the link for more details.

Add the below dependency to your app-level build.gradle file to use GSON.

Now update the data model class as below:

Update the Data Model Class

Update the User Adapter

After updating the data model class. You need to update these two lines in the onBindViewHolder method.

Update the MainActivity.kt using GSON

Update the MainActivity.kt as below:

Now, if you have noticed the JSON parsing using the function is just reduced to one line after updating the data model class as per the JSON string.

Run the application and see the output after changing the number of lines to one.

Output

It is the same as above but using the GSON library.

Summary

In this article, you have learned about the JSON parsing in Android with functions and with third party library GSON.

The form you have selected does not exist.

Enter your email and we will send you the PDF guide:
Enter your email and we will send you the PDF guide