Android开发之Json的解析

原文链接: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();
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值