在Android开发中,一般的APP都是通过获取服务器端的数据来更新UI,从服务器获取到的数据可以是Json,它的数据量要比XML要小,这里解析中国天气网上获取的数据,虽然已经不再更新了,但用来学习还是可以的,为了方便我直接到数据通过txt保存到本地,它的数据可以通过这个页面获取:http://m.weather.com.cn/data/101280601.html
目录结构
其实解析也很简单,关键代码
private void json3() {
InputStream inputStream = null;
String weatherinfo = null;
try {
System.out.println("-------------------------------->json3");
inputStream = getResources().getAssets().open("weather.txt");
weatherinfo = convertStreamToString(inputStream);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// 获取JSONObject对象
JSONObject jsonobject = null;
JSONObject jsoncity = null;
try {
jsonobject = new JSONObject(weatherinfo);
jsoncity = new JSONObject(jsonobject.getString("weatherinfo"));
WeatherInfo info = new WeatherInfo();
info.city = jsoncity.getString("city");
info.cityid = jsoncity.getString("cityid");
info.city_en = jsoncity.getString("city_en");
info.date = jsoncity.getString("date");
info.date_y = jsoncity.getString("date_y");
info.fchh = jsoncity.getString("fchh");
info.fl[0] = jsoncity.getString("fl1");
info.fl[1] = jsoncity.getString("fl2");
info.fl[2] = jsoncity.getString("fl3");
info.fl[3] = jsoncity.getString("fl4");
info.fl[4] = jsoncity.getString("fl5");
info.fl[5] = jsoncity.getString("fl6");
info.fx[0] = jsoncity.getString("fx1");
info.fx[1] = jsoncity.getString("fx2");
info.img[0] = jsoncity.getString("img1");
info.img[1] = jsoncity.getString("img2");
info.img[2] = jsoncity.getString("img3");
info.img[3] = jsoncity.getString("img4");
info.img[4] = jsoncity.getString("img5");
info.img[5] = jsoncity.getString("img6");
info.img[6] = jsoncity.getString("img7");
info.img[7] = jsoncity.getString("img8");
info.img[8] = jsoncity.getString("img9");
info.img[9] = jsoncity.getString("img10");
info.img[10] = jsoncity.getString("img11");
info.img[11] = jsoncity.getString("img12");
info.img_single = jsoncity.getString("img_single");
info.img_title[0] = jsoncity.getString("img_title1");
info.img_title[1] = jsoncity.getString("img_title2");
info.img_title[2] = jsoncity.getString("img_title3");
info.img_title[3] = jsoncity.getString("img_title4");
info.img_title[4] = jsoncity.getString("img_title5");
info.img_title[5] = jsoncity.getString("img_title6");
info.img_title[6] = jsoncity.getString("img_title7");
info.img_title[7] = jsoncity.getString("img_title8");
info.img_title[8] = jsoncity.getString("img_title9");
info.img_title[9] = jsoncity.getString("img_title10");
info.img_title[10] = jsoncity.getString("img_title11");
info.img_title[11] = jsoncity.getString("img_title12");
info.img_title_single = jsoncity.getString("img_title_single");
info.temp[0] = jsoncity.getString("temp1");
info.temp[1] = jsoncity.getString("temp2");
info.temp[2] = jsoncity.getString("temp3");
info.temp[3] = jsoncity.getString("temp4");
info.temp[4] = jsoncity.getString("temp5");
info.temp[5] = jsoncity.getString("temp6");
info.tempF[0] = jsoncity.getString("tempF1");
info.tempF[1] = jsoncity.getString("tempF2");
info.tempF[2] = jsoncity.getString("tempF3");
info.tempF[3] = jsoncity.getString("tempF4");
info.tempF[4] = jsoncity.getString("tempF5");
info.tempF[5] = jsoncity.getString("tempF6");
info.weather[0] = jsoncity.getString("weather1");
info.weather[1] = jsoncity.getString("weather2");
info.weather[2] = jsoncity.getString("weather3");
info.weather[3] = jsoncity.getString("weather4");
info.weather[4] = jsoncity.getString("weather5");
info.weather[5] = jsoncity.getString("weather6");
info.week = jsoncity.getString("week");
info.wind[0] = jsoncity.getString("wind1");
info.wind[1] = jsoncity.getString("wind2");
info.wind[2] = jsoncity.getString("wind3");
info.wind[3] = jsoncity.getString("wind4");
info.wind[4] = jsoncity.getString("wind5");
info.wind[5] = jsoncity.getString("wind6");
info.img_title[0] = jsoncity.getString("img_title1");
System.out.println("json = " + info.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
我把天气信息单独封装出来
package com.example.jsondemo.domain;
import java.util.Arrays;
public class WeatherInfo {
public String city;
public String cityid;
public String city_en;
public String date;
public String date_y;
public String fchh;
public String fl[];
public String fx[];
public String img[];
public String img_single;
public String img_title[];
public String img_title_single;
public String index;
public String index48;
public String index48_d;
public String index48_uv;
public String index_ag;
public String index_cl;
public String index_co;
public String index_d;
public String index_ls;
public String index_tr;
public String index_uv;
public String index_xc;
public String st[];
public String temp[];
public String tempF[];
public String weather[];
public String week;
public String wind[];
public WeatherInfo() {
fl = new String[6];
fx = new String[2];
img = new String[12];
img_title = new String[12];
st = new String[6];
temp = new String[6];
tempF = new String[6];
weather = new String[6];
wind = new String[6];
}
@Override
public String toString() {
return "weatherinfo [city=" + city + ", cityid=" + cityid
+ ", city_en=" + city_en + ", date=" + date + ", date_y="
+ date_y + ", fchh=" + fchh + ", fl=" + Arrays.toString(fl)
+ ", fx=" + Arrays.toString(fx) + ", img="
+ Arrays.toString(img) + ", img_single=" + img_single
+ ", img_title=" + Arrays.toString(img_title)
+ ", img_title_single=" + img_title_single + ", index=" + index
+ ", index48=" + index48 + ", index48_d=" + index48_d
+ ", index48_uv=" + index48_uv + ", index_ag=" + index_ag
+ ", index_cl=" + index_cl + ", index_co=" + index_co
+ ", index_d=" + index_d + ", index_ls=" + index_ls
+ ", index_tr=" + index_tr + ", index_uv=" + index_uv
+ ", index_xc=" + index_xc + ", st=" + Arrays.toString(st)
+ ", temp=" + Arrays.toString(temp) + ", tempF="
+ Arrays.toString(tempF) + ", weather="
+ Arrays.toString(weather) + ", week=" + week + ", wind="
+ Arrays.toString(wind) + "]";
}
}
解析出来的结果
这些网站可以对Json数据进行格式化,看起来更方便
如果格式有错,会有提示,方便我们修改,有一点需要注意,创建文件保存Json数据时不能有换行
以下是错误的,这是为了我们方便理解
{
"weatherinfo": {
"city": "深圳",
"city_en": "shenzhen",
"date_y": "2014年3月4日",
"date": "",
"week": "星期二",
"fchh": "11",
"cityid": "101280601",
"temp1": "20℃~15℃",
"temp2": "20℃~15℃",
"temp3": "21℃~16℃",
"temp4": "19℃~16℃",
"temp5": "20℃~15℃",
"temp6": "19℃~14℃",
"tempF1": "68℉~59℉",
"tempF2": "68℉~59℉",
"tempF3": "69.8℉~60.8℉",
"tempF4": "66.2℉~60.8℉",
"tempF5": "68℉~59℉",
"tempF6": "66.2℉~57.2℉",
"weather1": "多云",
"weather2": "小雨",
"weather3": "小雨",
"weather4": "阴转小雨",
"weather5": "小雨",
"weather6": "小雨转阴",
"img1": "1",
"img2": "99",
"img3": "7",
"img4": "99",
"img5": "7",
"img6": "99",
"img7": "2",
"img8": "7",
"img9": "7",
"img10": "99",
"img11": "7",
"img12": "2",
"img_single": "1",
"img_title1": "多云",
"img_title2": "多云",
"img_title3": "小雨",
"img_title4": "小雨",
"img_title5": "小雨",
"img_title6": "小雨",
"img_title7": "阴",
"img_title8": "小雨",
"img_title9": "小雨",
"img_title10": "小雨",
"img_title11": "小雨",
"img_title12": "阴",
"img_title_single": "多云",
"wind1": "微风",
"wind2": "微风",
"wind3": "东风3-4级",
"wind4": "微风",
"wind5": "微风",
"wind6": "微风",
"fx1": "微风",
"fx2": "微风",
"fl1": "小于3级",
"fl2": "小于3级",
"fl3": "3-4级",
"fl4": "小于3级",
"fl5": "小于3级",
"fl6": "小于3级",
"index": "较舒适",
"index_d": "建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。",
"index48": "较舒适",
"index48_d": "建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。",
"index_uv": "弱",
"index48_uv": "最弱",
"index_xc": "不宜",
"index_tr": "适宜",
"index_co": "舒适",
"st1": "20",
"st2": "15",
"st3": "21",
"st4": "15",
"st5": "21",
"st6": "14",
"index_cl": "适宜",
"index_ls": "适宜",
"index_ag": "极易发"
}
}
以下格式是才是正确的
{"weatherinfo":{"city":"深圳","city_en":"shenzhen","date_y