Learn JSON Basics in Android

Before reading the article, if you are the beginner just take out to learn more about JSON integration with Android.

This article will cover the following,

  • What is JSON?
  • Why JSON and Structures?
  • How and When to use JSON?

Let’s start,

What is JSON?


JSON basically stands for Javascript Object Notation is a lightweight data-interchange format that is easy access to read & write to human and machine to parse and generate and the best alternative of XML. These properties make JSON an ideal data-interchange language.

Why JSON?


JSON is preferable because of the following reasons

    • XML is heavier than JSON
    • XML Uses  tags  to describe  the user data and tags increase  the size of data
    • To Parse XML, We have to use  XPath which is an overhead  removed in  JSON, because JSON  is native to Javascript

JSON Structures


JSON can contain two structured types:

  • Object
  • Array

JSON Object


JSON object consists of name/value pairs, it begins with  ‘ { ‘ and ends with ‘ } ‘, each name is followed by ‘ : ‘ “ (colon) and name/value pairs are separated by ‘ ‘ (comma).

JSON object

Example

{

“employee”: {

“name”:      “Havard”,

“salary”:      56000,

}

}

JSON array


Array is an unordered collection of values, an array begins with ‘ [ ‘ and ends with ‘ ] ‘. Values are separated by ‘, ‘ (comma).

JSON Array

 

Example

[“Sunday”, “Monday”, “Tuesday”,  “Wednesday”, “Thursday”, “Friday”]

How and when to use JSON?


      • Transfer the  data to and from a server
      • Performs asynchronous data calls without needed a page refresh
      • Working with data stores
      • Compile and save user data for local storage

Expert Designed : Complete Android Topics (Free PDF Download)

Included standard library Using JSON APIs

In Android platform, includes the Json.org  library that allows creating and processing  JSON files

So when you talk about JSON simply the data transfer from client to the server in text format, Here  Mostly prefers to open source libraries like GSON for  JSON Processing. These libraries are easier to use, faster and more flexibility.

Example of  Reading JSON

To convert the JSON string into a JSON object is also simple. Use the following coding for the activity

import org.json.JSONArray;

import org.json.JSONObject;

String jsonString = readJsonObjectFromSomeWhere();

try {

JSONObject obj = new JSONObject(jsonString);

catch (Exception e) {

e.printStackTrace();

}

 Write the JSON

Just create the JSON object or array and use the toString( ) method.

public void writeJSON()

{

JSONObject obj = new JSONObject();

try {

obj.put(“name”, “Jas”);

obj.put(“score”, new Integer(100));

obj.put(“current”, new Double(132.32));

} catch (JSONException e)

{

e.printStackTrace();

}

System.out.println(obj);}

 

Although simple to understand and use, JSON is a very flexible and useful way to transfer data between applications and computers, then you’ll no doubt find that JSON is an essential tool in your toolbox.

Check out : Develop your own Android App