Android JSON Parser - Androhub

JSON Parser Banner

Android JSON Parser

JSON stands for JavaScript Object Notation.It is an independent data exchange format and is the best alternative for XML. JSON is very light weight, structured, easy to parse and much human readable.

In this tutorial we are going to learn how to parse JSON in android.

In this tutorial we will use Http Requests since we will work on server. So, if you are not familiar how to use Http Requests then you can check Http Request tutorial.

JSON – Elements

An JSON file consist of many components. Here is the table defining the components of an JSON file and their description −

ComponentDescription
Array([)In a JSON file , square bracket ([) represents a JSON array.
Objects({)In a JSON file, curly bracket ({) represents a JSON object.
KeyA JSON object contains a key that is just a string. Pairs of key/value make up a JSON object.
ValueEach key has a value that could be string , integer or double e.t.c.

The Sample JSON

Following is the sample JSON that we are going to parse in this tutorial. This is very simple JSON which gives us list of  information like name, location and phone number array.

In this tutorial we will parse data from local json file and also from server file.

1. Local JSON file that will be in .txt or .json extension kept in assests folder.

2. Server file that is my blogs recent posts JSON data.

In general all the JSON nodes will start with a square bracket or with a curly bracket. The difference between [ and { is, the square bracket ([) represents starting of an JSONArray node whereas curly bracket ({) represents JSONObject. So while accessing these nodes we need to call appropriate method to access the data.

If your JSON node starts with [, then we should use getJSONArray() method. Same as if the node starts with {, then we should use getJSONObject() method.

JSON Methods

There are several methods provided by JSON class for better parsing JSON files. These methods are listed below −

MethodDescription
get(String name)This method just Returns the value but in the form of Object type.
getBoolean(String name)This method returns the boolean value specified by the key.
getDouble(String name)This method returns the double value specified by the key.
getInt(String name)This method returns the integer value specified by the key.
getLong(String name)This method returns the long value specified by the key.
length()This method returns the number of name/value mappings in this object.
names()This method returns an array containing the string names in this object.

Example

In this tutorial, we are going to learn how to Parse JSON data from local Json file and Server Json data.

VIDEO DEMO

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. (I kept my main activity name as MainActivity.java).

2. As we are fetching the JSON by making HTTP calls, we need to add INTERNET permission in our AndroidManifest.xml file. Open AndroidManifest.xml and add the following permission.

3. Open res ⇒ values ⇒ strings.xml and add below string values. This are some strings that i used in this tutorial.

4. Now, for local Json file create a dummyjson.txt or dummyjson.json and copy from its location and paste it under assests directory.

5. For reading this local JSON file from assests folder i had created a method that will read the data from text file and return me as String.

6. Open your activity_main.xml file and add this code for basic layout of this tutorial. In this layout we have two buttons for executing different json task and one listview for displaying parsed data.

7. Now, for showing parsed data we need custom listview and for custom listview we need custom layout. So create a new layout naming custom_listview.xml and add the following code to it.

You can see how to make Simple and Custom ListView if you are new.

8. Now, we have to create a class that will be the setter and getter for parsed data for custom ListView. In this class i had set the setter and getter for both local json file and server json file, so that we can used as per our need.

9. For custom adapter create a class naming JSONData_Adapter.java. This will inflate custom layout over listview. In this i had used if and else for determining from which the adapter is called like “LOCAL” or “SERVER”. And according to it i displayed data.

10. Now for fetching JSON data we have to parse the data in proper format. First check the start of Json data i.e. ‘[‘ or ‘{‘ and parse according to it.

So, if Json data is starting from ‘{‘ means this is JSON Object then use :

If Json data is starting from ‘[‘ means JSON Array then use :

For Parsing JSON data from Local file i used this method :

Similarly we can parse the Server JSON data.

11. Finally add the following code to your MainActivity.java. In this activity I used two different methods for loading JSON data from Local file and from Server file. And I used AsyncTask  for processing JSON data. I had added comments to each line to make it more understandable.

12. Finally, all done  –  run the app and you will get the output as shown in video.

Thanks. :)

 

Post comment

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