Advertisement
ashish2py

simple JSON data parsing > android

Oct 31st, 2011
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.85 KB | None | 0 0
  1. package com.json.main;
  2.  
  3. import org.json.JSONException;
  4. import org.json.JSONObject;
  5. import org.json.JSONTokener;
  6.  
  7. import android.app.Activity;
  8. import android.os.Bundle;
  9. import android.widget.TextView;
  10.  
  11. public class JSONtestActivity extends Activity {
  12.         /** Called when the activity is first created. */
  13.         @Override
  14.         public void onCreate(Bundle savedInstanceState) {
  15.             super.onCreate(savedInstanceState);
  16.             setContentView(R.layout.main);
  17.             //TextView tv1 = (TextView)findViewById(R.id.TextView01);
  18.             //tv.setText("Hey see this,its working..");
  19.            
  20.            
  21.             try {
  22.                            
  23.                             /* Inflate TextView from the layout */
  24.                            
  25.                             TextView tv = (TextView)findViewById(R.id.TextView01);
  26.                            
  27.                             /* JSON data considered as an example. Generally this data is obtained
  28.                             from a web service.*/
  29.                            
  30.                             String json = "{"+
  31.                                             " \"name\" :\"ashish\", " +
  32.                                             " \"message\" :\"hey see this\", " +
  33.                                             " \"place\" :\"gurgaon\", " +
  34.                                             " \"date\" :\"aaj ka date hai \", " +
  35.                                             "  }";
  36.                            
  37.      
  38.                             JSONObject object = (JSONObject) new JSONTokener(json).nextValue();
  39.                             String name = object.getString("name");
  40.                             String message = object.getString("message");
  41.                             String place = object.getString("place");
  42.                             String date = object.getString("date");
  43.                            
  44.                             tv.setText("Name : " + name + "\n \n");
  45.                             tv.setText("Message" + message + "\n \n");
  46.                             tv.setText("Place" + place + "\n \n ");
  47.                             tv.setText("Data" + date + "\n \n");
  48.                            
  49.      
  50.             } catch (JSONException e) {
  51.                     e.printStackTrace();
  52.             }
  53.             catch(Exception ex){
  54.                     ex.printStackTrace();
  55.                     }
  56.            
  57.             }
  58.    
  59. }
  60.  
  61. /*
  62. here is XML >
  63.  
  64. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  65.     android:orientation="vertical"
  66.     android:layout_width="fill_parent"
  67.     android:layout_height="fill_parent">   
  68.  
  69.  
  70.     <TextView
  71.         android:id="@+id/TextView01"
  72.         android:layout_width="fill_parent"
  73.         android:layout_height="wrap_content"
  74.         android:text="No data"/>
  75.  
  76. </LinearLayout>
  77.  
  78. */
  79.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement