Click here to Skip to main content
15,885,113 members
Articles / Mobile Apps / Android

Android Parsing JSON

Rate me:
Please Sign up or sign in to vote.
4.10/5 (8 votes)
14 Jul 2017CPOL1 min read 11.4K   3   1
In this article, I will share how to parse JSON using JSON-Java in Android, however the snippet is not limited to Android only.

Ref : http://www.androidlearner.com/2017/07/android-using-json.html

When developing application at some 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 libraries 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 everything is ok.
  9. Now you are ready to use JSON library in your project.

Examples:

To parse rates from the below JSON text.

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

Java
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);
  }
This article was originally posted at http://www.androidlearner.com/feeds/posts/default

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
PraiseThanks Pin
EdwardEtheridge19647-Jul-17 7:36
EdwardEtheridge19647-Jul-17 7:36 

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.