Introduction
- JSON is faster than XML.
- JSON uses arrays.
- JSON is very easy to read and write.
- The loading and parsing time of JSON is less than XML loading and parsing time.
- The size of data in JSON data is less than XML data.
[{"id":"102","name":"amir","salary":"500000"},{"id":"103","name":"Ashwani","salary":"6000000"}]}
Step 1
Create a project like this:

Step 2
Create an XML file and write the following:
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:paddingLeft="@dimen/activity_horizontal_margin"
- android:paddingRight="@dimen/activity_horizontal_margin"
- android:paddingTop="@dimen/activity_vertical_margin"
- android:paddingBottom="@dimen/activity_vertical_margin"
- tools:context=".MainActivity"
- android:background="#f610">
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/textView1"
- android:textStyle="bold"
- android:textColor="#ffffff"
- android:textSize="15dp"
- />
- </RelativeLayout>
Step 3
Create a Java class file and write the following:
- package com.jsonparsingexample2;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.widget.TextView;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- public class MainActivity extends Activity
- {
- String ParsingDta = "{\"Employee\":[{\"id\":\"101\",\"name\":\"sonoo jaiswal\",\"salary\":\"50000\"},{\"id\":\"102\",\"name\":\"vimal jaiswal\",\"salary\":\"600000\"}]}";
- TextView textView1;
- String str = "";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- textView1 = (TextView) findViewById(R.id.textView1);
- try
- {
- JSONObject jsonObject = new JSONObject(ParsingDta);
- JSONArray jsonArray = jsonObject.getJSONArray("Employee");
- for (int i = 0; i < jsonArray.length(); i++) {
- JSONObject jsonObject1 = jsonArray.getJSONObject(i);
- String id = jsonObject1.getString("id").toString();
- String name = jsonObject1.getString("name").toString();
- String salary = jsonObject1.getString("salary").toString();
- str += "\n Employee" + i + "\n name:" + name + "\n id:" + id + "\n salary:" + salary + "\n";
- textView1.setText(str);
- }
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- }
Step 4
Android Manifest.xml file
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.jsonparsingexample2"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="7"
- android:targetSdkVersion="16" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.jsonparsingexample2.MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
- </manifest>
Step 5
Data of JSONArray

Sam HobbsPosted Nov 1, 2013, 7:24 PM
Is JSON as easy and efficient to use in all languages as it is in JavaScript?