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. <TextView
  12. android:layout_width="wrap_content"
  13. android:layout_height="wrap_content"
  14. android:id="@+id/textView1"
  15. android:textStyle="bold"
  16. android:textColor="#ffffff"
  17. android:textSize="15dp"
  18. />
  19. </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. public class MainActivity extends Activity
  10. {
  11. String ParsingDta = "{\"Employee\":[{\"id\":\"101\",\"name\":\"sonoo jaiswal\",\"salary\":\"50000\"},{\"id\":\"102\",\"name\":\"vimal jaiswal\",\"salary\":\"600000\"}]}";
  12. TextView textView1;
  13. String str = "";
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. textView1 = (TextView) findViewById(R.id.textView1);
  19. try
  20. {
  21. JSONObject jsonObject = new JSONObject(ParsingDta);
  22. JSONArray jsonArray = jsonObject.getJSONArray("Employee");
  23. for (int i = 0; i < jsonArray.length(); i++) {
  24. JSONObject jsonObject1 = jsonArray.getJSONObject(i);
  25. String id = jsonObject1.getString("id").toString();
  26. String name = jsonObject1.getString("name").toString();
  27. String salary = jsonObject1.getString("salary").toString();
  28. str += "\n Employee" + i + "\n name:" + name + "\n id:" + id + "\n salary:" + salary + "\n";
  29. textView1.setText(str);
  30. }
  31. } catch (JSONException e) {
  32. e.printStackTrace();
  33. }
  34. }
  35. @Override
  36. public boolean onCreateOptionsMenu(Menu menu) {
  37. // Inflate the menu; this adds items to the action bar if it is present.
  38. getMenuInflater().inflate(R.menu.main, menu);
  39. return true;
  40. }
  41. }
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. <uses-sdk
  7. android:minSdkVersion="7"
  8. android:targetSdkVersion="16" />
  9. <application
  10. android:allowBackup="true"
  11. android:icon="@drawable/ic_launcher"
  12. android:label="@string/app_name"
  13. android:theme="@style/AppTheme" >
  14. <activity
  15. android:name="com.jsonparsingexample2.MainActivity"
  16. android:label="@string/app_name" >
  17. <intent-filter>
  18. <action android:name="android.intent.action.MAIN" />
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>
Step 5
Data of JSONArray
JsonArrayData