JSON stands for JavaScript Object Notation. JSON is a light weight data interchange format in a text format so it can be easily parsed and read by machines and humans.
Parsing JSON data is important in Android. Create a new project in Android by using the following steps:
Step 1:
Step 2:
Step 3:
Step 4:
Step 5:
JSON uses the following two types of different structures:
- Collection of name/value pair
- Array
Example of collection of name/value pair: The { (curly brace) represents the JSON object.
- {"Employee" :{
-
- "id":"1",
- "name":"David",
-
- "address":"Delhi",
-
- "zip":"110002"}
-
- }
Example of collection of array: The [ (square bracket ) represents the JSON array.
- {
- "Employee": [
- {
- "id": "1",
- "name": "David",
- "address": "Delhi",
- "zip": "110002"
- },
- {
- "id": "2",
- "name": "John",
- "address": "Mumbai",
- "zip": "400001"
- },
- {
- "id": "3",
- "name": "Mathew",
- "address": "Delhi",
- "zip": "110003"
- },
- {
- "id": "4",
- "name": "Mary",
- "address": "Delhi",
- "zip": "110010"
- }]
- }
Complete Java class to parse JSON data.
- {
- "Employee": [
- {
- "id": "1",
- "name": "David",
- "address": "Delhi",
- "zip": "110002"
- },
- {
- "id": "2",
- "name": "John",
- "address": "Mumbai",
- "zip": "400001"
- },
- {
- "id": "3",
- "name": "Mathew",
- "address": "Delhi",
- "zip": "110003"
- },
- {
- "id": "4",
- "name": "Mary",
- "address": "Delhi",
- "zip": "110010"
- }]
- }
- package com.test.ppandey.jsonparse;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.TextView;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- public class MainActivity extends ActionBarActivity
- {
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- TextView output = (TextView) findViewById(R.id.textView1);
- String strJson = "{\"Employee\" :[ {" + " \"id\":\"1\",\n" + "\"name\":\"David\",\n" + "\"address\":\"Delhi\", \n" + "\"zip\":\"110002\"}, {\n" + "\"id\":\"2\",\n" + "\"name\":\"John\",\n" + "\"address\":\"Mumbai\",\n" + "\"zip\":\"400001\"}, {\n" + "\"id\":\"3\",\n" + "\"name\":\"Mathew\",\n" + "\"address\":\"Delhi\",\n" + "\"zip\":\"110003\"}, {\n" + "\"id\":\"4\",\n" + "\"name\":\"Mary\",\n" + "\"address\":\"Delhi\",\n" + "\"zip\":\"110010\"} \n" + "]}";
- String data = "";
- try
- {
- JSONObject jsonRootObject = new JSONObject(strJson);
-
- JSONArray jsonArray = jsonRootObject.optJSONArray("Employee");
-
- for (int i = 0; i < jsonArray.length(); i++)
- {
- JSONObject jsonObject = jsonArray.getJSONObject(i);
- int id = Integer.parseInt(jsonObject.optString("id").toString());
- String name = jsonObject.optString("name").toString();
- String address = jsonObject.optString("address").toString();
- String zip = jsonObject.optString("zip").toString();
- data += "Employee " + i + " : \n id= " + id + " \n Name= " + name + " \n Address= " + address + " \n zip=" + zip + "\n ";
- }
- output.setText(data);
- }
- catch (JSONException e)
- {
- e.printStackTrace();
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
-
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item)
- {
-
-
-
- int id = item.getItemId();
-
- if (id == R.id.action_settings)
- {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
In above code, we created string containing JSON data and object of class JSONObject.
- JSONObject jsonRootObject = new JSONObject(strJson);
And pass string containing JSON data to it.
Second step is to parse the JSON.
- JSONArray jsonArray = jsonRootObject.optJSONArray("Employee");
- int id = Integer.parseInt(jsonObject.optString("id").toString());
- String name = jsonObject.optString("name").toString();
- String address = jsonObject.optString("address").toString();
- String zip = jsonObject.optString("zip").toString();
Run the application and we get the JSON data as in the following.