Following file my source code
MainActivity.java
- package com.example.jsonparcer;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
BufferedReader bReader = null;
TextView name,date,hours;
Button data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name=(TextView) findViewById(R.id.name);
date=(TextView) findViewById(R.id.date);
hours=(TextView) findViewById(R.id.hours);
data=(Button) findViewById(R.id.data);
data.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
InputStream work = getApplicationContext().getResources()
.openRawResource(R.raw.work);
bReader=new BufferedReader(new InputStreamReader(work));
StringBuilder quesString = new StringBuilder();
String aJsonLine = null;
while ((aJsonLine = bReader.readLine()) != null) {
quesString.append(aJsonLine);
}
Log.d(this.getClass().toString(), quesString.toString());
String workjson=quesString.toString();
JSONObject quesObj = new JSONObject(quesString.toString());
JSONObject TimeTable = quesObj.getJSONObject("TimeTable");
name.append(TimeTable.getString("name"));
date.append(TimeTable.getString("date"));
hours.append(TimeTable.getString("time"));
} catch (Exception e){
} finally {
try {
bReader.close();
} catch (Exception e) {
Log.e("", e.getMessage().toString(), e.getCause());
}
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
AndroidManifest.xml
<?xml
version="1.0"
encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jsonparcer"
android:versionCode="1"
android:versionName="1.0"
>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19"
/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
>
<activity
android:name="com.example.jsonparcer.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>
activity_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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:textSize="20dp"
android:layout_height="wrap_content"
android:text="@string/Employ"
/>
<TextView
android:id="@+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_alignLeft="@+id/name"
android:layout_below="@+id/name"
android:layout_marginTop="23dp"
android:text="@string/Date"
/>
<TextView
android:id="@+id/hours"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_below="@+id/date"
android:layout_marginTop="26dp"
android:text="@string/Time"
/>
<Button
android:id="@+id/data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="126dp"
android:text="@string/Data"
/>
</RelativeLayout>
strings.xml
<?xml
version="1.0"
encoding="utf-8"?>
<resources>
<string
name="app_name">Jsonparcer</string>
<string
name="action_settings">Settings</string>
<string
name="hello_world">Hello
world!</string>
<string
name="Employ">Ename
:</string>
<string
name="Date">Date
:</string>
<string
name="Time">WHours
:</string>
<string
name="Data">Data</string>
</resources>
work.txt
{
"TimeTable":{
"name":"Vamsi",
"date":"8-07-2014",
"time":"8hours"
}
}
Above codes are my source code for parsing json object using android . After running above code following screenshot will be generated
After button pressing it will retrieve data from work.txt next it will show using Textview on screen