JsonParsing of JsonObject in Android Using Android Studio

Introduction

 
This article explains JsonParsing in Android. Android Studio is used to create the sample.
 
JSON is an acronym for JavaScript Object Notation. JSON is used for data exchange and it is a very condense data exchange format. JSON is used for storing data in files and it is a very easy method to parse data. Android provides support to parse JSON objects and arrays. 
 
Advantages of JSON over XML
  • JSON is faster than XML.
  • It uses arrays.
  • JSON is very easy to read and write.
JSON Object
 
A JSON Object has a key/value pair. The keys are of string type and the values are of JSON types. The { braces represent the JSON object.
 
Example of JsonObject
{"employee":
{"name":"amir",
  "salary":56000,
  "married":false}}"; 
 
Step 1
 
Create a project like this:
 
Create JsopnParseExample
 
Step 2
 
Create an XML file and write this. In this XML file you will use a textView for holding the data.
  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.    android:background="#ab3400"  
  10.     tools:context=".MainActivity">  
  11.    
  12.     <TextView  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:id="@+id/textView1"  
  16.         android:text="Amir"  
  17.         android:textSize="30dp"  
  18.         android:textStyle="bold"  
  19.         />  
  20. </RelativeLayout> 
Step 3
 
Create a Java class file and write the following.
 
In this class file, you will first change the JavaScript to string format. Now the string to the constructor and get the JsonObject by calling getJsonObject(). After entering it into the Jsonobject you can easily traverse the data. After traversing you will show the data in a textView. When you parse data the JSON object always throws an Exception called JsonException that you need to catch to maintain normal flow. In this, you will use the try and catch block. In the try block, you will write the following code to do the parsing.   
  1. package com.jsonparsingexample1;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.text.TextUtils;  
  5. import android.util.Log;  
  6. import android.view.Menu;  
  7. import android.widget.TextView;  
  8. import org.json.JSONArray;  
  9. import org.json.JSONException;  
  10. import org.json.JSONObject;  
  11. public class MainActivity extends Activity   
  12. {  
  13.  TextView textView1, textView2;  
  14.  String name;  
  15.  int id;  
  16.  public static final String ParsingData = "{\"employee\":{\"name\":\"amir\",\"salary\":56000,\"married\":false}}";  
  17.  @Override  
  18.  protected void onCreate(Bundle savedInstanceState)   
  19.  {  
  20.   super.onCreate(savedInstanceState);  
  21.   setContentView(R.layout.activity_main);  
  22.   textView1 = (TextView) findViewById(R.id.textView1);  
  23.   try   
  24.   {  
  25.    JSONObject jsonObject = new JSONObject(ParsingData).getJSONObject("employee");  
  26.    String name = jsonObject.getString("name");  
  27.    int salary = jsonObject.getInt("salary");  
  28.    String status = jsonObject.getString("married");  
  29.    String str = "Employee Name:" + name + "\nEmployee:" + salary + "\nstatus:" + status;  
  30.    textView1.setText(str);  
  31.   }   
  32.   catch (JSONException e) {}  
  33.  }  
  34.  @Override  
  35.  public boolean onCreateOptionsMenu(Menu menu)   
  36.  {  
  37.   getMenuInflater().inflate(R.menu.main, menu);  
  38.   return true;  
  39.  }  
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.jsonparsingexample1"  
  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.jsonparsingexample1.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26. </manifest> 
Step 5
 
Parse data using JSON
 
firstactivity


Similar Articles