Android Programming : Static JSON Parsing in Android

JSON data structure is very popular for structuring text. It’s a stand-alone JavaScript Object. It has replaced XML as object representation due to it’s simplicity. JSON notation is not only readable by JavaScript, but most languages today have some kind of JSON library for parsing JSON.

JSON unlike XML, is easily parseable. Parsing of JSON is easier to implement and less prone to errors than a XML implementation.

In this blog, we will do android development for static JSON parsing using Android JSON Parsing libraries in Android Studio. This will be really useful for android developers who wants to learn more about JSON integration with android.

Properties of JSON Data Structure

  • JSON stands for JavaScript Object Notation.
  • JSON has very lightweight format or structure.
  • JSON is used for data-interchange with server.
  • It can be easily read by human.
  • JSON is language independent. Because of language in-dependency we can program JSON in any language whether it’s Java or C/C++.
  • It is the best and widely used alternative of XML.

How JSON Looks Like?

Following is the sample JSON code that we are going to parse in this blog. This is a simple JSON string which give us array of hackpundit articles data. Here each object contain three attributes id, blog_name and publish_date.

Above JSON Structure has two parts:

  • JSON Object

    • It is represented by curly brackets”{ }“.
    • It contains Key-Value pairs.
    • In the below image key with their respective values are shown. Key are id, blog_name and publish_date.
    • Key-Value pairs are separated by colon ” : “.
    • JSON objects does not have any name.
  • JSON Array

    • It contains set of objects.
    • JSON array are represented by square brackets ” [ “.
    • JSON array have a key name. In the below image JSON array key name is Blogs” .

In the below image you can see the JSON Array and JSON Objects:

Android Programming Static JSON Parsing Array and Objects

Android Programming Static JSON Parsing Array and Objects

Android Development : Creating Android Project for Static JSON Parsing


In this android programming tutorial there are two activities:

  • The first activity is to design the front end, which is user view. Means how user will view our app.
  • The second activity is how to write the back end code for our  static JSON Parsing Android Application.

So lets gets started with our static JSON parsing android App:

  • Open your Android Studio.
  • Click on Start a New Android Studio Project.
  • Write your Application Name JsonParse and leave other fields blank as it is, then click NEXT.
Android Programming Static JSON Parsing Activity

Android Programming Static JSON Parsing Activity

  • Select the Minimum SDK API 15: Android 4.0.3(IceCreamSandwich)”I selected API 15 because it covers 94% device and it has almost all the features. If you want to cover 100% device then you can select API 8: Android 2.2(Froyo). But it has limitation on features. After selecting your API click on NEXT.
  • Select the blank activity and click NEXT.
  • Write the activity name “MainActivity” and leave everything as it is. Click Finish.
  • After clicking Finish, it takes around 5 minutes to build your basic hello world app.

Android Development Tutorial : Designing Front End or User View

  • Go to app -> res -> layout -> activity_main.xml.
  • Pick 2 textView elements.
  • Change the text and id as below:
HackPundit BlogstextView
Large texttextView1
Android Programming :  Static JSON Parsing TextView and ID

Android Programming : Static JSON Parsing TextView and ID

  • Below is the code for Activity_main.xml  file:

Android Programming Static JSON Parsing Activity User View

Android Programming Static JSON Parsing Activity User View

  • Great! we are done with the design.

Android Development Tutorial : Android Static JSON Parsing Back End

  • Go to the java file in which we will do coding for the action performed by app.
  • Go to app -> Java -> com.abhinavhackpundit.jsonparse -> .
Android Programming :

Android Programming :

  • Below is the code for file:

  • Below is the explanation of each highlighted line because that java code is behind the android static JSON parsing activity.
  • Line 18: Here we have cast or set the text for textView element. So that the data we fetch from JSON code will be shown in the textView(output) variable.
  • Line 20: We declare a variable strJson of type String. We store the JSON data in  this variable.
  • We write the complete JSON code in single line because Android Studio does not support multi line code.
  • Line 21: We declare a variable data of type String. Initialize with ” “. We store the parsed data from JSON code in this variable.
  • Line 22: We have created try – catch block to handle exceptions or unexpected exceptions.
  • Line 23: Here we have created the instance variable(jsonRootObject) of JSONObject class. By passing strJson variable as parameter we initialized that object with strJson.
  • Line 26: Here we have created the instance variable(jsonArray) of JSONArray class. jsonRootObject.optJSONArray(“Blogs”)  method gets all the objects present in the JSON Array “Blogs
  • Line 29: Here we started the for loop, to print all the value of JSON Objects. This loops starts with Zero and continues until it will iterate all the values of JSON Array(In this JSON code we have 3 values).
  • Line 30: Here we have created another instance(jsonObject) of JSONObject. We store all the object of JSONArray into jsonObject. We pass i(index) parameter into jsonArray.getJSONObject so that it will get data or objects one by one.
  • Line 32: Here first jsonObject.optString(“id”) method will execute. This method fetch the value of key(id) and convert into  String using toString() method. After getting value from toString() method, Integer.parseInt() method parse into integer value and store in Integer variable id.
  • We use toString() method to convert into String. Suppose we get the “NULL” value, then toString() method will convert into blank ” “. This will be understood by Integer.parseInt() method and convert to zero without any exception.
  • Line 33: Here first jsonObject.optString(“blog_name”) method will execute. This method will fetch the value of key(blog_name) and convert the value into string using toString() method. Here we don’t need to parse because the value is already in the required format which is String. After that the value is stored in string variable blog_name.
  • Line 34: Here first jsonObject.optString(“publish_date”) method will execute. This method will fetch the value of key(publish_dateand convert it into string using toString() method. Here we don’t need to parse because the value is already in the required format which is string. After that the value is stored in String variable publish_date.
  • Line 36: Here all the data is appended into one string and stored into the String variable data.
  • Line 38: output.setText(data) prints the data in the textView.
  • Line 39: Here it will catch the unexpected exceptions.
  • printStackTrace() method tell us where and on which line exception occurs and why.


We are ready to run our static JSON parsing android app. To run our app I used Genymotion Emulator as it’s fast. Also its performance is better than default Android emulator. If you have not already installed I highly recommend Genymotion emulator. You can install from here: Android Emulator Genymotion Step by Step Installation.

Running the Android App

  • Click on Genymotion device manager. After selecting your custom device in Genymotion device manager window, click START.
  • Click on Run.
  • Choose Genymotion device and click OK.
  • Now you can see your android static JSON parsing app running.
Android Development Tutorial : Json Parsing App Output

Android Development Tutorial : Json Parsing App Output

This is a beginner android development tutorial for parsing JSON. Google released android volley library which is very useful. If you want to know more about it read : Android Tutorial – JSON Parsing Using Volley Library

If I missed any point or you have any doubt regarding this blog just let me know in the comment section.

If you like my article please like our Facebook page and also follow us on Twitter so that you get regular updates. For regular updates you can also subscribe to with your email.

Please also share on Facebook and Twitter to help other android developers. Thank You..!