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:
Step 2
Create an XML file and write this. In this XML file you will use a textView for holding the data.
- <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"
- android:background="#ab3400"
- tools:context=".MainActivity">
-
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/textView1"
- android:text="Amir"
- android:textSize="30dp"
- android:textStyle="bold"
- />
- </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.
- package com.jsonparsingexample1;
- import android.os.Bundle;
- import android.app.Activity;
- import android.text.TextUtils;
- import android.util.Log;
- 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
- {
- TextView textView1, textView2;
- String name;
- int id;
- public static final String ParsingData = "{\"employee\":{\"name\":\"amir\",\"salary\":56000,\"married\":false}}";
- @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(ParsingData).getJSONObject("employee");
- String name = jsonObject.getString("name");
- int salary = jsonObject.getInt("salary");
- String status = jsonObject.getString("married");
- String str = "Employee Name:" + name + "\nEmployee:" + salary + "\nstatus:" + status;
- textView1.setText(str);
- }
- catch (JSONException e) {}
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu)
- {
- 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.jsonparsingexample1"
- 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.jsonparsingexample1.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
Parse data using JSON