Learn Android — JSON Parsing

The world’s most popular mobile OS — from phones and watches to cars and TVs.

Akshansh Dhing
Parsed Inc.
4 min readJul 6, 2018

--

Welcome to the Learn Android series. Here I explain you the why’s of the Android programming world instead of just the how’s to help you understand the core of Android programming. I’ll share with you explanations and code snippets on how to implement the most basic things in Android.

In the previous article, we learned a bit about how to connect our applications to the internet, including learning about permissions and multi-threading. Now, most of the data that we’ll receive from the internet (APIs, Firebase Databases, etc.) will be in NoSQL or JSON format.

Previous Article →

To extract any kind of information from the data we receive, we need to understand the structure of the data we receive from the internet. The data may contain more information than we actually need and we need to know how to break down the data and extract the relevant parts.

JSON stands for JavaScript Object Notation. Irrespective of the name, this format is not tightly coupled with the JavaScript programming language. This format is programming language independent and is basically used to organize information on the web.

Let’s have a look at a basic JSON response. Searching the internet for free APIs, we came across the Pokemon API (pretty cool, no?).
Here’s the link — https://pokeapi.co/

From the example on the main page, we copied a JSON result so as to learn how we can parse it.

The JSON response is completely enclosed between two curly braces. The JSON in it’s simplest bit, is in the format of key-value pairs. Now, with respect to the syntax, the key is on the left and the value on the right of the colon. This pattern is very important in computer science because it helps us call the data layer in easier and more logical manner.

{
"id": 1,
"name": "bulbasaur",
"base_experience": 64,
"height": 7,
"is_default": true,
"order": 1,
"weight": 69,
"abilities": [
{
"is_hidden": true,
"slot": 3,
"ability": {
"name": "chlorophyll",
"url": "http://pokeapi.co/api/v2/ability/34/"
}
}
],
"forms": [
{
"name": "bulbasaur",
"url": "http://pokeapi.co/api/v2/pokemon-form/1/"
}
]
}

In the above response, let’s look at a “key”. In the second line, we have a string key called “name” which references to a string value, “balbasaur”. The following comma is a delimiter for the next key-value pair.

So, we have “id” referencing to the integer value “1”. Then we have a “base experience” key referencing to the integer “64”, and more. These are simple key-value pairs with primitive data types.

But what about the following keys such as “forms” or “abilities”. What are the values type for these keys? Let’s look at the few JSON Components first —

  1. JSONObjects — is a set of key-value pairs. The keys in these can be either JSONObjects, JSONArrays, or any primitive data types.
  2. JSONArrays — is a set of JSONObjects, JSONArrays, or other primitive data types. These are similar to the arrays that we have in Java. We denote arrays using the square brackets ‘[]’. We use index values to extract items from the JSONArray.

We’ll traverse the JSON response above in Android and see how we can access the “chlorophyll” ability. Android already has built in support for JSON including it’s components. Now, the response we get from the internet is in the form of a String, so first we need to convert it into a JSONObject.

We created a function just to extract the JSON data (ability name in our case) from the response and here is the code below —

private void extractFromJSON(String response) {    try {
JSONObject baseJSONResponse = new JSONObject(response);
JSONArray abilities = baseJSONResponse.getJSONArray("abilities"); JSONObject abilityItem = abilities.getJSONObject(0); JSONObject ability = abilityItem.getJSONObject("ability"); String nameOfAbility = ability.getString("name"); } catch (JSONException e) {
e.printStackTrace();
}
}

Let’s look at the steps executed to get the data —

  1. We received a String response which we converted into JSONObject using the new keyword and named it baseJSONResponse.
  2. Then we get the “abilities” array using the getJSONArray() method and passing the name of the key as the parameter which will be stored in the JSONArray variable.
  3. Then using the JSONArray variable, we’ll get the first (and the only) item which we will store in a JSONObject data type. We passed in the index position 0 for the first item in the getJSONObject() method.
  4. In the JSONObject “abilityItem” there are 3 keys, and we need to access the 3rd one. We’ll again use the getJSONObject() method and pass in the key and store the value in another JSONObject variable.
  5. Finally, we’ve navigated the hierarchy and we’ll get the String “nameOfAbility” by using the getString() method.

Notice how we’ve been traversing the hierarchy similar to a tree. Using the nodes to get their children, and then using these children to further traverse deeper into the tree to get the original child’s child value.

We suggest you to try and traverse more JSON’s trees so that you can be confident when you actually need to implement it!

Stay tuned for regular updates. Follow me and Parsed Inc. to never miss another one!

Also, let’s become friends on LinkedIn, GitHub, Twitter and Facebook!

To learn more about me and my work, visit my website!

Follow ParsedInc. on Facebook, LinkedIn, and Instagram!

If you enjoyed this article, feel free to 👏👏👏 a few times and share with a friend to help it reach someone who needs to read it. Thanks!

--

--