Android JSONObject - JSON Parsing in Android With Examples

Android JSONObject is used for JSON parsing in android apps. In this tutorial we’ll discuss and implement a JSONObject in our android application to parse JSON data. JSON stands for JavaScript Object Notation.

What is JSON?

JSON is used for data interchange (posting and retrieving) from the server. Hence knowing the syntax and it’s usability is important. JSON is the best alternative for XML and its more readable by human.
JSON is language independent. Because of language in-dependency we can program JSON in any language (Java/C/C++).

A JSON response from the server consists of many fields. An example JSON response/data is given below. We’ll use it as a reference and implement it in our application.

      "company" : "Microsoft"
      "company": "Apple"
    "flag": true,

We’ve create a random JSON data string from this page. It’s handy for editing JSON data.

A JSON data consists of 4 major components that are listed below:

  1. Array: A JSONArray is enclosed in square brackets ([). It contains a set of objects
  2. Object: Data enclosed in curly brackets ({) is a single JSONObject. Nested JSONObjects are possible and are very commonly used
  3. Keys: Every JSONObject has a key string that’s contains certain value
  4. Value: Every key has a single value that can be of any type string, double, integer, boolean etc

Android JSONObject

We’ll create a JSONObject from the static JSON data string given above and display the JSONArray in a ListView. We’ll change the application name to the title string in the JSON data.

JSON Parsing in Android Example

Below image shows the android studio project for json parsing example. The project consists of the default activity and layout (with a ListView).


Android JSON Parsing Code

The activity_main.xml is given below.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android=""

The is given below.

package com.journaldev.jsonparsing;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    String json_string = "{n" +
            "  "title":"JSONParserTutorial",n" +
            "  "array":[n" +
            "    {n" +
            "    "company":"Google"n" +
            "    },n" +
            "    {n" +
            "      "company":"Facebook"n" +
            "    },n" +
            "    {n" +
            "    "company":"LinkedIn"n" +
            "    },n" +
            "    {n" +
            "      "company" : "Microsoft"n" +
            "    },n" +
            "    {n" +
            "      "company": "Apple"n" +
            "    }n" +
            "    ],n" +
            "    "nested":{n" +
            "    "flag": true,n" +
            "    "random_number":1n" +
            "    }n" +
    protected void onCreate(Bundle savedInstanceState) {
        try {
            ListView listView = (ListView) findViewById(;
            List<String> items = new ArrayList<>();
            JSONObject root = new JSONObject(json_string);
            JSONArray array= root.getJSONArray("array");
            for(int i=0;i<array.length();i++)
                JSONObject object= array.getJSONObject(i);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, items);
            if (listView != null) {
            JSONObject nested= root.getJSONObject("nested");
            Log.d("TAG","flag value "+nested.getBoolean("flag"));
        } catch (JSONException e) {

We’ve iterated through the JSONArray object and fetched the strings present in each child JSONObject and added them to a ArrayList that’s displayed in the ListView. The application name is changed using :


Android JSONObject Example Output

The output of the application is given below. You can see the title name changed in the ToolBar at the top.


Google has released a Volley Library for JSON Parsing. We’ll implement that in later tutorials. GSON is a Java library that converts Java Objects into JSON and vice versa.

This brings an end to android JSONObject tutorial. Our aim was to give a overview of JSON Parsing in android since JSON is the accepted standard these days for transmitting data between servers/web applications.

Android JSON parsing will be very handy when we develop applications that send and receive data from the server. You can download the Android JSON Parsing Project from the below link.

Reference: Official Documentation

By admin

Leave a Reply

%d bloggers like this: