Click here to Skip to main content
12,511,494 members (55,699 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

43.4K views
2 bookmarked
Posted

Parse JSON Data in Android

, 4 Aug 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

{
    "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.

//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.

//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)

Share

About the Author

Amanpreet Mukker
Software Developer
United States United States
No Biography provided

You may also be interested in...

Pro
Pro

Comments and Discussions

 
Questionhow to insert data in json file Pin
chandiwal21-May-15 18:10
memberchandiwal21-May-15 18:10 
QuestionIf JSON is for HTTP requests ... Pin
Artur Sharipov9-Aug-13 5:17
memberArtur Sharipov9-Aug-13 5:17 
AnswerRe: If JSON is for HTTP requests ... Pin
Amanpreet Mukker9-Aug-13 6:05
memberAmanpreet 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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160929.1 | Last Updated 4 Aug 2013
Article Copyright 2013 by Amanpreet Mukker
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid