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

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”

}

console.log(salary)

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”

}

]

console.log(Employees)

 

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”}]’;

console.log(JSON.parse(Empolyees))

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=”http://schemas.Android.com/apk/res/Android”

xmlns:tools=”http://schemas.Android.com/tools”

Android:layout_width=”match_parent”

Android:layout_height=”match_parent”

tools:context=”.Activity” >

<TextView

Android:id=”@+id/Employee”

Android:layout_width=”wrap_content”

Android:layout_height=”wrap_content”

Android:layout_marginLeft=”65dp”

Android:layout_marginTop=”55dp”

Android:layout_alignParentLeft=”true”

Android:layout_alignParentTop=”true”

Android:text=”TextView” />

</RelativeLayout>

Create Another File named src/activity_main.java. Then write the following code

import Android.os.Bundle;

import Android.support.v7.app.AppCompatActivity;

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;

@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// get the reference of TextView’s

employeeName =(TextView) findViewById(R.id.Name);

employeePosition =(TextView) findViewById(R.id.Position);

employeeSalary =(TextView) findViewById(R.id.Salary);

employeeBranch =(TextView) findViewById(R.id.Branch);

try{

// 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);}

catch

(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 ” ; }

output.setText(employee01);

 

This was how to parse multiple android json array.

 

 

Summary

 

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:

 

 

Leave a Reply

Your email address will not be published.