Gson – How to parse JSON Arrays, an array of arrays?

android json

Before the JSON array, you need to know, What is JSON? The complete form of JSON is “Javascript object nation.” We can say, in general, it is a system or format where you transfer your data from client to server and server to client.


We will discuss JSON arrays before telling how to parse multiple android json array. Then it will be easier to understand how to parse a JSON array.


What is JSON and JSON array?


You may think, what is he talking about? let me briefly tell you what JSON is,

JSON is a branch of javascript code that allows you to store data in many formats. As this is javascript code, you can easily use those codes on websites. In Android, you can also use those. Let me show you how it looks like,


employee01 = {

“name”: “A”,

“position”: “Programmer”,

“Salary” : “200000”



This one is a simple javascript code.


You may think of writing it in the script area of your HTML file. But when more than 20 employees it will be a big mass. That’s why you should always maintain JSON files.

Those files need a name with a JSON extension. Where you can store multiple data, let’s see, JSON array.


What is JSON Array?


Having more than one JSON data is called a JSON array, and you can have multiple nested data in your array. Let me show you in detailed code.


Employees = [


“Name” : “A”,

“Position” : “Programmer”,

“Salary” : “20000”,

“Team” : [“B”,”C”,”D”],

“Branch” : “BD”



“Name” : “F”,

“Position” : “Manager”,

“Salary” : “20000”,

“Team” : [“A”,”E”,”H”],

“Branch” : “BD”





But in JSON syntax, files are placed in string format. That’s where comes parse Function.

NB, JSON data comes with a double quotation mark (“), and that is stringified using a single quotation mark (‘).

you may check that in below


How to parse multiple JSON array in javascript?


Below we have shown an example to parse json array in javascript.


Empolyees = ‘[{“Name” : “A”,”Position” : “Programmer”,”Salary” : “20000”,”Team” : [“B”,”C”,”D”],”Branch” : “BD” },{“Name” : “F”,”Position” : “Manager”,”Salary” : “20000”,”Team” : [“A”,”E”,”H”],”Branch” : “BD”}]’;


in This code you can see, you have to use JSON. parse Function to see JSON array object or multiple JSON data.

Now you know how to parse JSON data. Let’s see how to parse JSON array in Android.


How to parse multiple JSON array in java?


You already know how to parse JSON array in javascript, but you must use it in Android, which is based on java. So, here you will see how you can parse JSON in java.


At first, you have to locate your JSON format file.

You have initialized your variable by using JSONParser class for java to parse. So code will be,

JSONParser <yourParserName> = new JSONParser();

You must parse content from that object by the parse() method.

JSONObject <yourObjectName> = (JSONObject) <yourParserName>.parse(new FileReader(“Drive:/File.json”));


Already you have parsed JSON value. Now just call keys and get printed the values. You can print that value, or if you want, you can store it in another variable. Code to do that is,


To store value,

String <variableName> = <yourObjectName>.get(“Name”);

To Print value,

System.out.println(“Name: ” +<yourObjectName>.get(“Name”));

The output will be “A”



android json




Now came the first One, How to parse multiple JSON arrays in Android. Let me show you below.


How To Parse Multiple Android Json Array?


Suppose you have done with a piece of sound knowledge in the above topics. Let me show you how to parse JSON array Android. To parse JSON in Android, you first have to have Android Studio installed. After that, you need to do a new project. You give a name as your wish.


Then, open the res folder then find the layout, and there you will get the activity_main.xml or main.xml file. and write these codes,


<?xml version = “1.0” encoding = “utf-8” ?>

<RelativeLayout xmlns:androclass=””




tools:context=”.Activity” >









Android:text=”TextView” />


Create Another File named src/ Then write the following code

import Android.os.Bundle;


import Android.widget.TextView;

import org.json.JSONException;

import org.json.JSONObject;

public class Activity_Main extends AppCompatActivity{

String Employee= ‘{“employee01” : {“Name” : “A”,”Position” : “Programmer”,”Salary” : “20000”,”Team” : [“B”,”C”,”D”],”Branch” : “BD” }}’;

String Name, Position, Salary, Branch;

TextView employeeName, employeePosition, employeeSalary, employeeBranch;


protected void onCreate(Bundle savedInstanceState){



// get the reference of TextView’s

employeeName =(TextView) findViewById(;

employeePosition =(TextView) findViewById(;

employeeSalary =(TextView) findViewById(;

employeeBranch =(TextView) findViewById(;


// Initilizing json object

JSONObject employeeDetail =new JSONObject(Employee);

// geting data from JSONObject which named employee

JSONObject employee = employeeDetail.getJSONObject(“employee01”);

// get employee value

name = employee01.getString(“Name”);

position = employee01.getString(“Position”);

salary = employee01.getString(“Salary”);

branch = employee01.getString(“Branch”);

// set employee name and salary in TextView’s

employeeName.setText(“Name: “+name);

employeeSalary.setText(“Salary: “+salary);}


(JSONException e)

{ e.printStackTrace();




That’s how you can parse json data in Android.

You have to change the try section with this code to parse the array.

// Create a JSON Object first

JSONObject employeeDetail =new JSONObject(Employee);

//Write this code to fetch JSON array,

JSONArray teamArray = employeeDetail.optJSONArray(“Employee”);

//make a loop to read all the data from array,

for(int i=0; i < teamArray.length(); i++){

JSONObject employee = teamArray.getJSONObject(i);

int id = Integer.parseInt(employee.optString(“id”).toString());

String name = employee.optString(“Name”).toString();

String position = employee.optString(“Position”).toString();

float salary = Float.ParseFloat(employee.optString(“Salary”).toString());

String branch = employee.optString(“Branch”).toString();

employee01 += “Node”+i+” : \n Name= “+ name +” \n Position= “+ position +” \n Salary= “+ salary +” \n Branch= ” + branch + “\n ” ; }



This was how to parse multiple android json array.





To Summarise, you already show how to parse multiple android json array. Now, please follow those steps carefully to get a brief knowledge of JSON and JSON arrays.



You may also be interested to know:



By Imran Chowdhury

Imran is a tech-savvy guy, love to share the hacks of technology related tips and tricks, and a certified learner from Android developer platform He has been in the industry since 2017. With his vast knowledge and hundreds of blog publications, he can solve any tech-related problem flawlessly. The main areas of expertise are android and ios app-related problems, tech-tutorial, and operating systems. He explains rocket-science-type difficult things easily so you can follow instructions and solve problems. He is currently working as the founder of Writing hack and some other blogs. His love for writing and solving others' problems sets him apart from other writers.

Leave a comment

Your email address will not be published. Required fields are marked *