访问的页面是:
北京天气
{"weatherinfo":{"city":"北京","cityid":"101010100","temp1":"15℃","temp2":"5℃","weather":"多云","img1":"d1.gif","img2":"n1.gif","ptime":"08:00"}}
用[]包裹的是JSONArray,这里的数据都是用{}包裹,所以都是JSONObject,里面还包括了一个key为weather的JSONObject
package com.jackie.jsontest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import net.callumtaylor.asynchttp.AsyncHttpClient;
import net.callumtaylor.asynchttp.response.StringResponseHandler;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
private Button btnJO;
private Button btnGS;
private AsyncHttpClient asyncHttpClient = new AsyncHttpClient("http://www.weather.com.cn/adat/cityinfo/101010100.html");
private String response;
private String city;
private String cityid;
private String temp1;
private String temp2;
private String weather;
private String img1;
private String img2;
private String ptime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
asyncHttpClient.get(new StringResponseHandler() {
@Override
public void onSuccess() {
response = getContent();
Log.d("jackie", response);
//返回的是:
// {"weatherinfo":
// {"city":"北京",
// "cityid":"101010100",
// "temp1":"15℃",
// "temp2":"5℃",
// "weather":"多云",
// "img1":"d1.gif",
// "img2":"n1.gif",
// "ptime":"08:00"}
// }
}
});
btnJO = (Button) findViewById(R.id.btnJO);
btnJO.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
parseJSONWithJSONObject(response);
}
});
}
private void parseJSONWithJSONObject(String response) {
try {
/*
分析一下返回的json数据,均使用的是大括号包裹,说明是JSONObject
*/
//得到第一层JSONObject
JSONObject jsonObject = new JSONObject(response);
//得到第二层JSONObject
JSONObject weatherInfo = jsonObject.getJSONObject("weatherinfo");
city = weatherInfo.getString("city");
cityid = weatherInfo.getString("cityid");
temp1 = weatherInfo.getString("temp1");
temp2 = weatherInfo.getString("temp2");
weather = weatherInfo.getString("weather");
img1 = weatherInfo.getString("img1");
img2 = weatherInfo.getString("img2");
ptime = weatherInfo.getString("ptime");
Log.d("jackie", "city: " + city);
Log.d("jackie", "cityid: " + cityid);
Log.d("jackie", "temp1: " + temp1);
Log.d("jackie", "temp2: " + temp2);
Log.d("jackie", "weather: " + weather);
Log.d("jackie", "img1: " + img1);
Log.d("jackie", "img2: " + img2);
Log.d("jackie", "ptime: " + ptime);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
GSON解析实际上是将json数据映射成java实体类
例如这里的数据:
{"weatherinfo":{"city":"北京","cityid":"101010100","temp1":"15℃","temp2":"5℃","weather":"多云","img1":"d1.gif","img2":"n1.gif","ptime":"08:00"}}
第一个大括号代表着最外围的类,它有一个成员变量,就是weatherinfo:后面的大括号,所以这里我们需要两个类,分别代表最外面的大括号和里面的大括号
/**
* 代表最外围的大括号的类
* Created by Law on 2015/11/29.
*/
public class GSONObject {
/*
这里注意,变量的名字weatherinfo一定要和json数据中的一模一样,大小写要区分!
*/
private WeatherInfo weatherinfo;
public WeatherInfo getWeatherinfo() {
return weatherinfo;
}
public void setWeatherinfo(WeatherInfo weatherinfo) {
this.weatherinfo = weatherinfo;
}
}
/**
* 代表weatherinfo:{}
* Created by Law on 2015/11/29.
*/
public class WeatherInfo {
private String city;
private String cityid;
private String temp1;
private String temp2;
private String weather;
private String img1;
private String img2;
private String ptime;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getCityid() {
return cityid;
}
public void setCityid(String cityid) {
this.cityid = cityid;
}
public String getTemp1() {
return temp1;
}
public void setTemp1(String temp1) {
this.temp1 = temp1;
}
public String getTemp2() {
return temp2;
}
public void setTemp2(String temp2) {
this.temp2 = temp2;
}
public String getWeather() {
return weather;
}
public void setWeather(String weather) {
this.weather = weather;
}
public String getImg1() {
return img1;
}
public void setImg1(String img1) {
this.img1 = img1;
}
public String getImg2() {
return img2;
}
public void setImg2(String img2) {
this.img2 = img2;
}
public String getPtime() {
return ptime;
}
public void setPtime(String ptime) {
this.ptime = ptime;
}
@Override
public String toString() {
return "WeatherInfo{" +
"city='" + city + '\'' +
", cityid='" + cityid + '\'' +
", temp1='" + temp1 + '\'' +
", temp2='" + temp2 + '\'' +
", weather='" + weather + '\'' +
", img1='" + img1 + '\'' +
", img2='" + img2 + '\'' +
", ptime='" + ptime + '\'' +
'}';
}
}
那么GSON解析就是这样的:
private void parseJSONWithGSON(String response) {
Gson gson = new Gson();
GSONObject gsonObject = gson.fromJson(response, GSONObject.class);
WeatherInfo weatherInfo = gsonObject.getWeatherinfo();
Log.d("jackie", "city: " + weatherInfo.getCity());
Log.d("jackie", "cityid: " + weatherInfo.getCityid());
Log.d("jackie", "temp1: " + weatherInfo.getTemp1());
Log.d("jackie", "temp2: " + weatherInfo.getTemp2());
Log.d("jackie", "weather: " + weatherInfo.getWeather());
Log.d("jackie", "img1: " + weatherInfo.getImg1());
Log.d("jackie", "img2: " + weatherInfo.getImg2());
Log.d("jackie", "ptime: " + weatherInfo.getPtime());
}