原文链接:http://blog.csdn.net/hudashi/article/details/8736050
通过JSONObject与JSONArray来解析json
我们可以通过JSONObject与
JSONArray
的getInt,getString,getDouble,getJSONArray,getJSONObject等函数来解析json.
先上图:
布局文件:
JavaBean:
public class Person {
private String name;
private int age;
private String[] phone;
private Address address;
private boolean married;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String[] getPhone() {
return phone;
}
public void setPhone(String[] phone) {
this.phone = phone;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
public boolean isMarried() {
return married;
}
public void setMarried(boolean married) {
this.married = married;
}
}
public class Address {
private String country;
private String province;
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
}
核心代码:
public class JsonDemo2Activity extends Activity implements OnClickListener {
private TextView tv_json_text;
private Button bt_parser_json;
private EditText et_parser_json;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv_json_text = (TextView) findViewById(R.id.tv_json_text);
bt_parser_json = (Button) findViewById(R.id.bt_parser_json);
et_parser_json = (EditText) findViewById(R.id.et_parser_json);
bt_parser_json.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String text = tv_json_text.getText().toString();
Person person = parserJson(text);
et_parser_json.setText("名字:" + person.getName() + " ");
et_parser_json.append("年龄:" + person.getAge() + " ");
}
/*
* 假设现在要解析这样一个json文本 { "phone" : ["12345678", "87654321","02887188812"],
* "name" : "yuanzhifei89", "age" : 100, "address" : { "country" : "china",
* "province" : "jiangsu" }, "married" : false }
*/
private Person parserJson(String text) {
try {
JSONObject jsonObject = new JSONObject(text);
Person person = new Person();
if (jsonObject.has("name")) {
person.setName(jsonObject.getString("name"));
}
if (jsonObject.has("age")) {
person.setAge(jsonObject.getInt("age"));
}
if (jsonObject.has("phone")) {
String[] phone = new String[3];
JSONArray jsonArray = jsonObject.getJSONArray("phone");
phone[0] = jsonArray.getString(0);
phone[1] = jsonArray.getString(1);
phone[2] = jsonArray.getString(2);
person.setPhone(phone);
}
if (jsonObject.has("address")) {
Address address = new Address();
JSONObject json_address = jsonObject.getJSONObject("address");
address.setCountry(json_address.getString("country"));
address.setProvince(json_address.getString("province"));
person.setAddress(address);
}
if (jsonObject.has("married")) {
person.setMarried(jsonObject.getBoolean("married"));
}
return person;
} catch (Exception e) {
throw new RuntimeException();
}
}
}