<pre name="code" class="java">1.JSON为对象
<pre name="code" class="html">{"name":"江疏影","age":3,"sex":"nv"}
//如果是JSON对象,那么创建JSONObject的对象
JSONObject object01 = new JSONObject(data);
//通过键,得到值
int age = object01.getInt("age");
String name = object01.getString("name");
String sex = object01.getString("sex");
//组装字符串
String result = "姓名:"+name+"\n性别:"+sex+"\n年龄:"+age;
//显示数据在TextView上面
tv.setText(result);
2.JSON为数组
<pre name="code" class="html"><pre name="code" class="html">[
{"name":"江疏影","age":3,"sex":"nv"},
{"name":"江疏影","age":2,"sex":"renyao"}
]
//定义StringBuffer
StringBuffer sb = new StringBuffer();
//如果当前的JSON数据是一个数组,那么创建JSONArray的对象
JSONArray array = new JSONArray(data);
//数组---遍历(循环)
//数组的长度 array.length();
//数组里面每一个元素array.getJSONObject(i);
for (int i = 0; i < array.length(); i++) {
//得到数组的每一个元素
JSONObject object = array.getJSONObject(i);
//得到数据
String name = object.getString("name");
String sex = object.getString("sex");
int age = object.getInt("age");
//组装字符串
sb.append("姓名:").append(name).append("\n");
sb.append("性别:").append(sex).append("\n");
sb.append("年龄:").append(age).append("\n");
sb.append("-------").append("\n");
}
//得到数据
tv.setText(sb.toString());
3.JSON为综合类型
创建MyBean
<pre name="code" class="html">public class MyBean {
private String date;
private String temperature;
private String weather;
private String weather_id;
private String week;
private String wind;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTemperature() {
return temperature;
}
public void setTemperature(String temperature) {
this.temperature = temperature;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
public String getWeather_id() {
return weather_id;
}
public void setWeather_id(String weather_id) {
this.weather_id = weather_id;
}
public String getWeek() {
return week;
}
public void setWeek(String week) {
this.week = week;
}
public String getWind() {
return wind;
}
public void setWind(String wind) {
this.wind = wind;
}
public MyBean(String date, String temperature, String weather,
String weather_id, String week, String wind) {
super();
this.date = date;
this.temperature = temperature;
this.weather = weather;
this.weather_id = weather_id;
this.week = week;
this.wind = wind;
}
@Override
public String toString() {
return "MyBean [date=" + date + ", temperature=" + temperature
+ ", weather=" + weather + ", weather_id=" + weather_id
+ ", week=" + week + ", wind=" + wind + "]";
}
代码实现
<pre name="code" class="html">//创建集合
ArrayList<MyBean> al = new ArrayList<MyBean>();
//创建JSONObject的对象
JSONObject object01 = new JSONObject(data);
//通过第一个数据得到对象
JSONObject object02 = object01.getJSONObject("result");
//通过第二个数据得到数组
JSONArray array01 = object02.getJSONArray("future");
//循环遍历
for (int i = 0; i < array01.length(); i++) {
//得到每一个对象
JSONObject object03 = array01.getJSONObject(i);
String xdate = object03.getString("date");
String xtemperature = object03.getString("temperature");
String xweather = object03.getString("weather");
String xweather_id = object03.getString("weather_id");
String xweek = object03.getString("week");
String xwind = object03.getString("wind");
//创建JavaBean的对象
MyBean bean = new MyBean(xdate, xtemperature, xweather, xweather_id, xweek, xwind);
//添加数据
al.add(bean);
}
//---------------------
StringBuffer sb = new StringBuffer();
//显示数据
for (int i = 0; i < al.size(); i++) {
MyBean bean = al.get(i);
sb.append("bean:"+bean.toString()).append("\n");
sb.append("------------").append("\n");
}
tv.setText(sb.toString());
4.开源框架 可以上github 原理其实就是工具类的封装,可以参见谷歌API!