最近刚刚学习Android开发,在看《第一行代码》最后的天气预报app,但是因为网站版本的变化,书中提供的天气预报api不能用了,于是就去百度apiStore上找了一个天气预报API。
返回数据是这样的:
我的心情是这样的
因为之前没有接触过json,所以看着非常晕,折腾了好几个小时才弄明白。
其实只需要一个格式转换工具,一切就很清楚了
转换完格式如下
如图所示,如果我想得到city中aqi的值,就这么写
jsonObject = new JSONObject(response);
//weather是个数组
JSONArray weather = jsonObject.getJSONArray("HeWeather data service 3.0");
//本例中只有一个内容,所以只需要0
JSONObject temp = weather.getJSONObject(0);
JSONObject aqi = temp.getJSONObject("aqi");
JSONObject city = aqi.getJSONObject("city");
re = city.getString("aqi");
最后附上天气api的地址:http://apistore.baidu.com/apiworks/servicedetail/478.html
转换json格式工具:http://jsonviewer.stack.hu/