Home Tutorials Hints & Tips

Getting Started in Android Development

Part 34 - A Simple JSON Example

Another format for parsing data is JSON or Java Script Object Notation. JSON is often referred to as a "fat free" alternative to XML. In comparison XML is heavy weight, but in principle, they are used to achieve the same task. The text below show some data in JSON format followed by the XML equivalent.

In JSON format:

   {"firstName":"John", "lastName":"Smith","salary":"50000"},
   {"firstName":"Jane", "lastName":"Roberts","salary":"75000"},
   {"firstName":"Steve", "lastName":"Johnson","salary":"25000"}

The same in XML format:


You can see why JSON is lightweight compared to XML and there are similarities. JSON still uses "key value pairs" to identify data and it's value.

In this example we will use this data and parse it using the Android JSON tools.

Normally, this sort of data would be downloaded by the Android device from the Internet and processed by the app. However, to ensure we focus on the core tasks at hand, the JSON data will be stored as a string within the code of the activity as show below:

String jsonData = "{\"employees\":["
   + "{\"firstName\":\"Steve\",\"lastName\":\"Johnson\",\"salary\":\"25000\"}]}";

A very simple layout has been defined with a single TextView that will display the data. The activity_json.xml file shown below:

In order to understand the code we first need to understand a little about JSON itself. Data inside of any square brackets [] is part of a JSON array. The data in curly brackets {}  are JSON objects.

The first task in the code is to define an object of the JSONObject class that is used the retrieve and hold the JSON data. In our example the string variable jsonData is passed to the JSONObject.

JSONObject jsonEmployeeData = new JSONObject(jsonData);

We then need to define an object of JSON array and pass the name of the array we are interested in. In example the array is called "employees".

JSONArray jsonArray = jsonEmployeeData.optJSONArray("employees");

A for loop is defined that will parse the entire JSON array.

for (int i = 0; i < jsonArray.length(); i++) {

Inside of the for loop we then need to retrieve the JSON objects we are interested in and store them as strings. The string data is used to concatenate the string "printData" value for the TextView output.

String firstName = jsonObject.optString("firstName").toString();
String lastName = jsonObject.optString("lastName").toString();
float salary = Float.parseFloat(jsonObject.optString("salary").toString());
printData += "Node" + i + " : \n First Name = " + firstName + " \n Last Name = " + lastName + " \n Salary = " + salary + " \n";

At the basic level, that really is all there is to it.

After this tutorial your JsonActivity.java file should look similar to the one below:

Your MainActivity.java file should look similar to the one below:

Your Android Manifest file should look similar to the one below:

A screenshot of the parsed JSON file is shown below:

Android JSON example screenshot

Download Download tutorial set 2

Privacy and Cookies Disclaimer Copyright
© 2015 - 2018 North Border Tech Training All rights reserved