1 of 13

JSON in Android

Mobile Application Development

2 of 13

JSON

  • JSON  - JavaScript Object Notation
  • one of the simplest data exchange formats to interchange the data from the server 
  • best alternative for XML. 
  • JSON is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays (or other serializable values). 
  • It is a common data format with diverse uses in electronic data interchange.

JSON in Android

2

https://en.wikipedia.org/wiki/JSON

3 of 13

JSON

  • JSON is a language-independent data format. 
  • It was derived from JavaScript, but many modern programming languages include code to generate and parse JSON-format data. 
  • JSON filenames use the extension .json

JSON in Android

3

https://en.wikipedia.org/wiki/JSON

4 of 13

JSON in Android

4

  • Generally, the JSON nodes will start with a square bracket ([) or with a curly bracket ({). 

  • the square bracket ([) represents the starting of a JSONArray node

  • curly bracket ({) represents a JSONObject 

  • so we need to call appropriate method to get the data.

Source:-https://www.tutlane.com/tutorial/android/android-json-parsing-with-examples

5 of 13

JSON - Data types

JSON's basic data types are:

  • Number (signed decimal number),String, Boolean
  • Array: an ordered list of zero or more elements. Arrays use square bracket notation with comma-separated elements.
  • Object: a collection of name–value pairs where the names (also called keys) are strings. Objects are delimited with curly brackets and use commas to separate each pair, while within each pair the colon ':' character separates the key or name from its value.
  • null: an empty value.

JSON in Android

5

6 of 13

JSON in Android

Android classes to access JSON in Android

  • JSONObject 
  • JSONArray
  • JSONStringer
  • JSONTokener
  • JSONException

 to parse the JSON data to get the required information in android applications.

JSON in Android

6

7 of 13

JSONArray

  • A dense indexed sequence of values. 
  • Values may be any mix of 
      • JSONObjects
      • other JSONArrays
      • Strings, Booleans, Integers, Longs, Doubles, 
      • null . 
  • Values may not be 
      • NaNsinfinities, or of any type not listed here.

JSON in Android

7

https://developer.android.com/reference/org/json/JSONArray

8 of 13

JSONArray

8

Constructors:

  • JSONArray() - Creates a JSONArray with no values.
  • JSONArray(String json) -

Creates a new JSONArray with values from the JSON string.

Methods:

JSONArray getJSONArray(int index)

JSONObject getJSONObject(int index)

Long getLong(int index)

String getString(int index)

JSONArray put(int index, int value)

JSONArray put(boolean value)

https://developer.android.com/reference/org/json/JSONObject

9 of 13

JSONObject - class

Constructor:

  • JSONObject() - Creates a JSONObject with no name/value mappings.
  • JSONObject(String json)

Creates a new JSONObject with name/value mappings from the JSON string.

JSON in Android

9

https://developer.android.com/reference/org/json/JSONObject

10 of 13

JSONObject - class

Methods:

int getInt(String name)

Returns the value mapped by name if it exists  or throws Exception otherwise.

JSONArray     getJSONArray(String name)

JSONObject    getJSONObject(String name)

JSONObject    put(String name, long value)

JSON in Android

10

https://developer.android.com/reference/org/json/JSONObject

11 of 13

Example

JSON in Android

11

https://www.digitalocean.com/community/tutorials/android-jsonobject-json-parsing

12 of 13

Example

JSON in Android

12

Courtesy:

https://www.digitalocean.com/community/tutorials/android-jsonobject-json-parsing

13 of 13

Example

JSON in Android

13

Courtesy:

https://www.digitalocean.com/community/tutorials/android-jsonobject-json-parsing