JSON Parsing:
JSON
Parsing Stands for JavaStript Object Notation, JSON is used for transmitting the data over remote Network.JSON are also used to parsed the data from the Same Network.
In
android, application uses JSON to transmit data over networks and JSON data is
parsed, while it is received from cloud servers and from some where else.
JSON
Parsing with Android Application in Detail:
JSON
provides data in form of object that is to be parsed while reading form source
and a JSON object is created, whenever data is to be transmitted over network
or any source to destination.
JSON Format:
JSON Object Format:
Here the data between Curly braces represents JSONObject.
JSONArray Format:
[
Name: "sunil",,
Address : "hyd",
Phone : 00000000
]
Here Data between square braces represents JSONArray.
Combination of JSONObject and JSONArray:
{ (JSONObject)
"Employee"
:
[(JSONArray)
{(JSONObject1)
"
Name
"
:
"sunil"
,
"Address"
:
"hyd"
,
"phone"
: 9000000000
},
{(JSONObject2)
"
Name
"
:
"Tanil"
,
"Address"
:
"hyd"
,
"phone"
: 9000000001
}
]
}(End Of JSONObject)
JSONParsing Example Statically:
{
"employee":
[
{
"id":"100",
"name":"Sunil
Ambilpur",
"email":"sunil@gmail.com",
"address":"hyd",
"gender"
:"male",
"phone":{"mobile":"+91
0000000000",
"home":"00
000000",
"office":"00
000000"
}
]
}
Here Statically means we are storing the JSON data in the String.
Create Android Application:
- File >> New >> Android Application
- Enter Project name: JSONParsingApp
- Package: com.ambilpursunil.newapp
- Keep other default selections, click Next until you reach Finish

1.Simply Copy and Past the Code which is display below in the MainActivity.java
package com.ambilpursunil.newapp;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity4 extends Activity{
String
str = "{\"employee\":[{\"id\":\"100\",\"name\":\"Sunil Ambilpur\",\"email\":\"sunil@gmail.com\",\"address\":\"hyd\",\"gender\"
:\"male\",\"phone\":{\"mobile\":\"+91
0000000000\",\"home\":\"00
000000\",\"office\":\"00 000000\"}}]}";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView
tv = (TextView) findViewById(R.id.tvJSON);
try {
JSONObject
jo = new JSONObject(str);
JSONArray
ja = jo.getJSONArray("employee");
for(int i = 0; i<=ja.length(); i++){
JSONObject
job = ja.getJSONObject(i);
String
id = job.getString("id");
String
name = job.getString("name");
String
email = job.getString("email");
String
address = job.getString("address");
String
gender = job.getString("gender");
// Phone node is
JSON Object
JSONObject
phone = job.getJSONObject("phone");
String
mobile = phone.getString("mobile");
String
home = phone.getString("home");
String
office = phone.getString("office");
String
strr = "id"+id+"\n"+"name"+name+"\n"+"email"+email+"\n"+"address"+address+"\n"+"gender"+gender+"\n"+"mobile"+mobile+"\n"+"home"+home+"\n"+"office"+office;
tv.setText(strr);
}
}
catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
2.Simple Copy and Past the below code: actvitiy_main.xml
<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">
<TextView
android:id="@+id/tvJSON"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="76dp"
android:layout_marginTop="65dp"
android:text="TextView" />
</RelativeLayout>
3.Copy and past the code for manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ambilpursunil.newapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".ManiActivity"
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>
When our application launch data will be visible on the screen.
Please Send Your Comments To ambilpura.sunil@gmail.com
Stay Tune For Next Tutorial... RestFull Web Services Using JSON In Android:
No comments:
Post a Comment