Skip to main content

Android Parsing JSON

When developing application at some of time you need to communicate with web service for data exchange. If your communication uses JOSN(JavaScript Object Notation) for data transfer. You may start finding the easiest library available for the JSON however there are plenty of library for JSON library for JAVA.
The one which I found suitable for my need is :
JSON-java License : MIT
Author : Sean Leary

In this article I will share how to parse JSON using JSON-java in android however the snippet is not limited to android only

Getting Started

1. Download the library from :
    download the source as ZIP from the github

2. Extract the Files from the zip
3. Open Android-Studio
4. Right click the Java folder navigate to New->Package

5. Select the destination directory as main\java , click on OK button

6. Enter package name as org.json and click on OK button

7. Now copy all the JSON-java .java from the source folder and copy them to Android-Studio
    org.json  package.

8. Build the android project to ensure that every thing is ok.
9. Now you are ready to use JSON library in your project

Examples :-
To parse rates from the below JSON text.


Java Code to iterate over all the rates.

String jsonText = "{\"base\":\"USD\",\"date\":\"2017-02-17\",\"rates\":{\"AUD\":1.3044,\"BGN\":1.8364,\"BRL\":3.0918,\"CAD\":1.3079,\"CHF\":0.99878,\"CNY\":6.867,\"CZK\":25.372,\"DKK\":6.9797,\"GBP\":0.80488,\"HKD\":7.7614,\"HRK\":6.9869,\"HUF\":289.5,\"IDR\":13332.0,\"ILS\":3.7061,\"INR\":67.1,\"JPY\":112.75,\"KRW\":1150.0,\"MXN\":20.474,\"MYR\":4.453,\"NOK\":8.3235,\"NZD\":1.3905,\"PHP\":50.055,\"PLN\":4.0662,\"RON\":4.2463,\"RUB\":58.185,\"SEK\":8.8712,\"SGD\":1.4165,\"THB\":34.995,\"TRY\":3.673,\"ZAR\":13.085,\"EUR\":0.93897}}";

  // First get Complete Json
  JSONObject parseJson = new JSONObject(jsonText);
  // get the Rate object
  JSONObject rates = parseJson.getJSONObject("rates");
  // Iterate for all the rates
  Iterator itr = rates.keys();
  // loop
  while (itr.hasNext()) {
   String data = (String);



Popular posts from this blog

Xamarin : Simple Calculator Tutorial Visual Studio C#

Simple Calculator in Android (Xamarin) using VisualStudio C#In Brief: This article will help you on building a simple calculator in android Xamarin with simple steps using C# Language in Visual Studio. Detail:
Building this simple calculator will let you learn how to work with the Android Buttons, Text View and simple functions. The UI is not so beautiful but is a great way to learn the basic functionalities.

Create Custom View in android MATRIX EFFECT CANVAS tutorial

Create Custom View in android MATRIX EFFECT CANVAS tutorial
Hi all as I stared my development with android I always wanted to create a Custom view or rather I want a canvas where I can draw freely. To learn about canvas I decided to create matrix rain effect which I can add to my android layout. Here is the complete tutorial which I have post. Hopping it will be use full to someone.

You can find the link to android project at the bottom of the post

Android Transparent status bar

Recently I wanted to get a transparent status bar so that my app background blends smoothly with the status bar . Below method worked