Click here to Skip to main content
15,881,882 members
Articles / Mobile Apps / Android
Tip/Trick

Parse JSON Data in Android

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
4 Aug 2013CPOL 61.9K   2   3
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)


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhow to insert data in json file Pin
chandiwal21-May-15 18:10
chandiwal21-May-15 18:10 
QuestionIf JSON is for HTTP requests ... Pin
Artur Sharipov9-Aug-13 5:17
Artur Sharipov9-Aug-13 5:17 
AnswerRe: If JSON is for HTTP requests ... Pin
Amanpreet Mukker9-Aug-13 6:05
Amanpreet Mukker9-Aug-13 6:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.