JSONParsing of JSONArray in Android

Introduction

 
This article explains the JsonParsing of JsonArray in Android. Android Studio is used to create the sample.
 
JSON is the acronym for JavaScript Object Notation. JSON is a very condense data exchange format. JSON is used for storing data in files and it is easier than XML parsing.
 
Advantages of JSON over XML
  • 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.
JSON array example
 
{"Employee":
    [{"id":"102","name":"amir","salary":"500000"},{"id":"103","name":"Ashwani","salary":"6000000"}]}
 
Step 1
 
Create a project like this:
 
Create JsopnParseExample
 
Step 2
 
Create an XML file and write the following:
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity"  
  10.     android:background="#f610">  
  11.    
  12.     <TextView  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:id="@+id/textView1"  
  16.         android:textStyle="bold"  
  17.         android:textColor="#ffffff"  
  18.         android:textSize="15dp"  
  19.         />  
  20.    
  21. </RelativeLayout> 
Step 3
 
Create a Java class file and write the following:
  1. package com.jsonparsingexample2;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.view.Menu;  
  5. import android.widget.TextView;  
  6. import org.json.JSONArray;  
  7. import org.json.JSONException;  
  8. import org.json.JSONObject;  
  9.   
  10. public class MainActivity extends Activity   
  11. {  
  12.   
  13.  String ParsingDta = "{\"Employee\":[{\"id\":\"101\",\"name\":\"sonoo jaiswal\",\"salary\":\"50000\"},{\"id\":\"102\",\"name\":\"vimal jaiswal\",\"salary\":\"600000\"}]}";  
  14.  TextView textView1;  
  15.  String str = "";  
  16.  @Override  
  17.  protected void onCreate(Bundle savedInstanceState) {  
  18.   super.onCreate(savedInstanceState);  
  19.   setContentView(R.layout.activity_main);  
  20.   textView1 = (TextView) findViewById(R.id.textView1);  
  21.   
  22.   try   
  23.   {  
  24.    JSONObject jsonObject = new JSONObject(ParsingDta);  
  25.    JSONArray jsonArray = jsonObject.getJSONArray("Employee");  
  26.    for (int i = 0; i < jsonArray.length(); i++) {  
  27.   
  28.     JSONObject jsonObject1 = jsonArray.getJSONObject(i);  
  29.     String id = jsonObject1.getString("id").toString();  
  30.     String name = jsonObject1.getString("name").toString();  
  31.     String salary = jsonObject1.getString("salary").toString();  
  32.     str += "\n Employee" + i + "\n name:" + name + "\n id:" + id + "\n salary:" + salary + "\n";  
  33.     textView1.setText(str);  
  34.    }  
  35.   } catch (JSONException e) {  
  36.    e.printStackTrace();  
  37.   }  
  38.  }  
  39.  @Override  
  40.  public boolean onCreateOptionsMenu(Menu menu) {  
  41.   // Inflate the menu; this adds items to the action bar if it is present.  
  42.   getMenuInflater().inflate(R.menu.main, menu);  
  43.   return true;  
  44.  }  
  45. }
Step 4
 
Android Manifest.xml file
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.jsonparsingexample2"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.jsonparsingexample2.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.                 <category android:name="android.intent.category.LAUNCHER" />  
  22.             </intent-filter>  
  23.         </activity>  
  24.     </application>  
  25. </manifest> 
Step 5
 
Data of JSONArray
 
JsonArrayData


Similar Articles