Skip to content

Parsing JSON Data In Android

  • by

API development is the primary area where JSON is most widely used. Irrespective of which language (ie, JAVA, PHP, C etc) you use to develop or which platform are you in (i.e, Computer, Mobile etc) JSON data format is considered as the best method to use in data interchange. In this article i will explain about parsing JSON data in Android from the angle of an API development.

Basic Philosophy

In the stages of data transfer the parsing comes at the receiver end. Receiver should be able to receive and process the data. This processing means extracting the values from JSON data. This functionality is known as JSON parsing. JSON parser is the one which is performing parsing functionality.

Parsing normally done by traversing through the JSON string from top to down. The parser will go through entire node. Each node is converted either to an object or an array. And converting entire JSON string to program variables is the key functionality of a JSON parser. Only after this conversion we can use JSON values in our program. In this way communication between two entirely different platforms can comes into reality. In this way JSON parsing is the back pillar of data communication.

Being JSON data format is an universal hence it is language independent and generic. Converting it into program is the duty of JSON parser.

Criterias For Parsing

There are some criterias to in order to perform JSON parsing which are,

  1. There should be a JSON parser to perform parsing
  2. JSON must be well formed i.e, syntax should be error free
  3. JSON parsing should be handled programmatically using JSON error handler classes

Two Most Important Things In JSON Parsing

  • JSONObject – a class that represents object in a JSON string
  • JSONArray – an array that represents array in a JSON string

JSONObject

JSONObject works exactly in the same way like normal class object in java. i.e, values are retrieved by . operator. in java we know each variables are type dependent.

Each value you extract will have a type which is very important. When see this example you will get it easily

For example, When you want to a string value from JSONObject syntax should be like this,

Getting string value

  String mark = jsonObject.getString("mark");

Getting integer value

  int mark = jsonObject.getInt("mark");

Getting boolean value

  Boolean passed = jsonObject.getBoolean("passed");

Getting double value

  double mark = jsonObject.getDouble("mark");

JSONArray

As a normal array in java value in array is accessed by index. A JSONArray is with fixed size and each index has a value.

Getting string value

  String mark = jsonArray.getString("mark");

Getting integer value

  int mark = jsonArray.getInt("mark");

Getting boolean value

  Boolean passed = jsonArray.getBoolean("passed");

Getting double value

  double mark = jsonArray.getDouble("mark");

Data types are based on the difference in data. if you are able to represent it in a string format then it can also be json format too. this is the power of json.

Example Of Parsing With Simple JSON Data

json_structure_sample_new

/* String response = "{'profile':{'firstName':'Smith','lastName':'John','age':'30','gender':'Male','address':{'street':'20th2ndStreet','city':'NewYork','state':'NY','postal_code':'10003'},'contact':[{'type':'Home','number':'(735)754-0100'},{'type':'Office','number':'(725)854-0750'}]}}"; // Let's assume it has JSON string of profile

*/

  JSONObject wrapperObject = new JSONObject(response);  
  String firstNameTxt= wrapperObject .getString("firstName").toLowerCase();
  String lastNameTxt= wrapperObject .getString("lastName").toLowerCase();
  String ageTxt= wrapperObject .getString("age").toLowerCase();
  String genderTxt= wrapperObject .getString("gender").toLowerCase();


  JSONObject addressObject = new JSONObject(wrapperObject.getString("address"));


   String streetTxt = addressObject.getString("street").toLowerCase();
   String cityTxt = addressObject.getString("city").toLowerCase();
   String stateTxt = addressObject.getString("state").toLowerCase();
   String postalCodeTxt = addressObject.getString("postal_code").toLowerCase();


   JSONArray contactArray = new JSONArray(wrapperObject.getString("contact"));

   for (int i = 0; i < contactArray.length(); i++) {

       JSONObject contactObject = new JSONObject(contactArray.get(i).toString());
       String typeTxt = contactObject.getString("type").toLowerCase();
       String numberTxt = contactObject.getString("number").toLowerCase();

   }

Leave a Reply

Your email address will not be published. Required fields are marked *