package com.jasonex;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class jasonEx extends Activity {
/** Called when the activity is first created. */
private final static String jstring= "{\"person\":{\"name\":\"A\",\"age\":30,\"children\":[{\"name\":\"B\",\"age\":5}," + "{\"name\":\"C\",\"age\":7},{\"name\":\"D\",\"age\":9}]}}";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tx1=(TextView)findViewById(R.id.textView1);
TextView tx2=(TextView)findViewById(R.id.textView2);
TextView tx3=(TextView)findViewById(R.id.textView3);
try
{
JSONObject person=((new JSONObject(jstring)).getJSONObject("person"));
String name=person.getString("name");
Log.d("Name","name is"+name);
tx1.setText("person name is"+name);
tx2.setText(name+"is"+person.getInt("age")+"years old");
tx3.setText(name+"has"+person.getJSONArray("children").length()+"chidren");
}catch(JSONException e)
{
e.printStackTrace();
}
}
}
No comments:
Post a Comment