Android Parsing JSON

sapan


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 : https://github.com/stleary/JSON-java
    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.



{  
   "base":"USD",
   "date":"2017-07-04",
   "rates":{  
      "AUD":1.3144,
      "BGN":1.7227,
      "BRL":3.3034,
      "CAD":1.2975,
      "CHF":0.96486,
      "CNY":6.8017,
      "CZK":23.018,
      "DKK":6.5504,
      "GBP":0.77341,
      "HKD":7.8082,
      "HRK":6.5326,
      "HUF":271.56,
      "IDR":13366.0,
      "ILS":3.516,
      "INR":64.738,
      "JPY":113.25,
      "KRW":1151.8,
      "MXN":18.212,
      "MYR":4.2975,
      "NOK":8.3546,
      "NZD":1.3736,
      "PHP":50.509,
      "PLN":3.737,
      "RON":4.0416,
      "RUB":59.315,
      "SEK":8.5207,
      "SGD":1.383,
      "THB":34.015,
      "TRY":3.5565,
      "ZAR":13.228,
      "EUR":0.88082
   }
}

Java Code to iterate over all the rates.


1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
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) itr.next();
   System.out.println(data);

  }

Post a Comment

0Comments
Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !