Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Mobile / Android

Parse JSON Data in Android

4.00/5 (1 vote)
4 Aug 2013CPOL 62.5K  
Parsing JSON data in Android is simple and seamless

Introduction

This tip explains how do parse JSON data in any Android application using JSONObject class.

Using the Code

So, here is an example of the data that I'm trying to parse. It's a dummy list of two chemistry elements with values, inside a JSON file. I have named the file chem_elements.json and it's in my assets folder inside my IDE (using Android Studio). Here is the dummy data:

C++
{
    "Hydrogen": {
        "symbol" : "H",
        "atomic_number" : 1,
        "atomic_weight" : 1.00794
    },
    "Helium": {
        "symbol" : "He",
        "atomic_number" : 2,
        "atomic_weight" : 4.002602
    }
}  

Here is the Java method that will parse the JSON file and will return a JSON object. Make sure the JSON file is in assets folder inside your Android project, because otherwise you will get a file not found exception.

Java
//Following imports are necessary for JSON parsing 
import org.json.JSONException;
import org.json.JSONObject;

//Method that will parse the JSON file and will return a JSONObject 
public JSONObject parseJSONData() {
        String JSONString = null;
        JSONObject JSONObject = null;
        try {

            //open the inputStream to the file 
            InputStream inputStream = getAssets().open("chem_elements.json");

            int sizeOfJSONFile = inputStream.available();

            //array that will store all the data 
            byte[] bytes = new byte[sizeOfJSONFile];

            //reading data into the array from the file
            inputStream.read(bytes);

            //close the input stream
            inputStream.close();

            JSONString = new String(bytes, "UTF-8");
            JSONObject = new JSONObject(JSONString);

        } catch (IOException ex) {
            ex.printStackTrace();
            return null;
        }
        catch (JSONException x) {
            x.printStackTrace();
            return null;
        }
        return JSONObject;
   } 

Now, we need to use the JSON object and retrieve the data from the file. As you can see in the raw JSON file, there is a hierarchy involved. There are elements (Hydrogen, Helium) and then each element has values. Here is the example that shows how to retrieve different values.

Java
//Get the JSON object from the data
JSONObject parent = this.parseJSONData();

//THis will store all the values inside "Hydrogen" in a element string  
String element = parent.getString("Hydrogen");

//THis will store "1" inside atomicNumber
String atomicNumber = parent.getJSONObject("Hydrogen").getString("atomic_number"); 

This is it, you can now retrieve any data inside the JSON file using the above calls.

Points of Interest

I always wanted to learn about JSON file format, but did not know it was this easy. :)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)