Android项目--Json解析

时间:2023-03-09 16:37:18
Android项目--Json解析

在过去的一段时间里,我希望做一个天气的应用,但是由于老版的天气接口已经不能用了。只能更新到2014年3月4日。

不过有些东西,哪来学习一下,也是可以的。

比如:http://m.weather.com.cn/data/101050101.html

{
"weatherinfo": {
"city": "哈尔滨",
"city_en": "haerbin",
"date_y": "2014年3月4日",
"date": "",
"week": "星期二",
"fchh": "",
"cityid": "",
"temp1": "-5℃~-17℃",
"temp2": "-6℃~-18℃",
"temp3": "-5℃~-16℃",
"temp4": "-4℃~-18℃",
"temp5": "-3℃~-17℃",
"temp6": "-4℃~-16℃",
"tempF1": "23℉~1.4℉",
"tempF2": "21.2℉~-0.4℉",
"tempF3": "23℉~3.2℉",
"tempF4": "24.8℉~-0.4℉",
"tempF5": "26.6℉~1.4℉",
"tempF6": "24.8℉~3.2℉",
"weather1": "多云转晴",
"weather2": "晴",
"weather3": "晴",
"weather4": "多云转晴",
"weather5": "多云转晴",
"weather6": "晴",
"img1": "",
"img2": "",
"img3": "",
"img4": "",
"img5": "",
"img6": "",
"img7": "",
"img8": "",
"img9": "",
"img10": "",
"img11": "",
"img12": "",
"img_single": "",
"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": "北风3-4级转小于3级",
"wind2": "北风3-4级转小于3级",
"wind3": "西南风3-4级转小于3级",
"wind4": "南风3-4级转小于3级",
"wind5": "东南风3-4级转小于3级",
"wind6": "东南风3-4级转小于3级",
"fx1": "北风",
"fx2": "北风",
"fl1": "3-4级转小于3级",
"fl2": "3-4级转小于3级",
"fl3": "3-4级转小于3级",
"fl4": "3-4级转小于3级",
"fl5": "3-4级转小于3级",
"fl6": "3-4级转小于3级",
"index": "寒冷",
"index_d": "天气寒冷,建议着厚羽绒服、毛皮大衣加厚毛衣等隆冬服装。年老体弱者尤其要注意保暖防冻。",
"index48": "寒冷",
"index48_d": "天气寒冷,建议着厚羽绒服、毛皮大衣加厚毛衣等隆冬服装。年老体弱者尤其要注意保暖防冻。",
"index_uv": "最弱",
"index48_uv": "弱",
"index_xc": "适宜",
"index_tr": "一般",
"index_co": "较不舒适",
"st1": "-6",
"st2": "-15",
"st3": "-8",
"st4": "-16",
"st5": "-7",
"st6": "-14",
"index_cl": "较不宜",
"index_ls": "基本适宜",
"index_ag": "极不易发"
}
}

不同的标签代表着不同的意思

在对这串json文件进行解析的话。

我们可以先将数据从网络上下载下来。

public String downloadWeatherInfo(String path) {
HttpURLConnection connection = null;// 表示HttpURLConnection连接
StringBuffer sb = new StringBuffer();// 表示字节流缓冲区
InputStream inputstream = null;// 表示输入字节流
InputStreamReader inputStreamReader = null;// 表示输入字符流
BufferedReader bufferedReader = null;// 表示字符流缓冲区
try {
URL url = new URL(path);// 将字符串地址解析成URL地址
connection = (HttpURLConnection) url.openConnection();// 打开连接
connection.setConnectTimeout();// 设置连接的时间
connection.setReadTimeout();// 设置超时时间
connection.setRequestMethod("GET");//网络设置请求方式
connection.connect();// 建立连接
int code = connection.getResponseCode();// 表示连接返回的请求码
if (code == ) {// 请求码200 表示连接成功
inputstream = connection.getInputStream();// 获取网络连接的数据流
inputStreamReader = new InputStreamReader(inputstream);// 实例化一个字符流对象,将读到的数据流转为字符流
bufferedReader = new BufferedReader(inputStreamReader);// 实例化一个字符流缓冲区,将转号的字符流先放到缓冲区中
String line = "";// 表示数据流的行
sb = new StringBuffer();// 实例化一个StringBuffer对象
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);// 一行一行的将缓冲字符流中的数据存放到StringBuffer对象中
}
connection.disconnect();// 关闭网络连接
} else {
connection.disconnect();// 关闭网络连接
}
} catch (Exception e) {
System.out.println("读取数据出现错误");
return "";
} finally {// 不管是否出现异常 都要执行关闭所有已经开启的流
try {
bufferedReader.close();
inputStreamReader.close();
inputstream.close();
connection.disconnect();
} catch (IOException e) {
System.out.println("IOException");
}
}
return sb.toString(); }

再传入接口

    /**
* 解析天气信息,把服务器获得的String串 Json 解析成天气信息放到list集合中
*/ public void parseWeather() {
listData = new ArrayList<Map<String, Object>>();
new Thread() {
public void run() {// http://m.weather.com.cn/data/101050101.html json对象
//实例化一个StringBuffer对象
String url = "http://m.weather.com.cn/data/"+mPath+".html";
// StringBuffer sb = new StringBuffer();
// //拼一个服务器地址
// sb.append(Constants.WEATHER_API_URL);sb.append(mPath);sb.append(".html");
//实例化一个天气的工具类
WeatherInfoTool tool = new WeatherInfoTool();
//服务器返回的String串
String strdata = tool.downloadWeatherInfo(url); // 根据path获得服务器的string串
// new JSON 在Android中 不需要倒入包 可以直接用是Android内置了
try {
JSONObject jsonObject = new JSONObject(strdata);//实例化一个jaon对象
JSONObject json = jsonObject.getJSONObject("weatherinfo");//得到下载数据的json对象
mCity = json.getString("city"); // 当前城市
mDate = json.getString("date_y");// 当前日期
// 其余五天日期需要算法计算
mWeather_Week = json.getString("week"); // 当前星期
// 其余五天星期需要算法计算
mWeather_Fl = json.getString("fchh"); // 当天风力
// 6天的温度
mWeather_Info = json.getString("temp1");
mWeather_Info1 = json.getString("temp2");
mWeather_Info2 = json.getString("temp3");
mWeather_Info3 = json.getString("temp4");
mWeather_Info4 = json.getString("temp5");
mWeather_Info5 = json.getString("temp6");
// 6天的天气的详情 去控制显示的图片
mWeather_CurrentDetails = json.getString("weather1");
mWeather_Details1 = json.getString("weather2");
mWeather_Details2 = json.getString("weather3");
mWeather_Details3 = json.getString("weather4");
mWeather_Details4 = json.getString("weather5");
mWeather_Details5 = json.getString("weather6");
// 除今天以外的 其他5天的信息
map = new HashMap<String, Object>();
map.put("weather_Details", mWeather_Details1);// 传入天气详情控制图片以及详情
map.put("weather_data", "2014年3月5日");// 控制日期
map.put("weather", mWeather_Info1);// 控制温度
map.put("weather_day", "星期三");// 控制星期
listData.add(map);
map = new HashMap<String, Object>();
map.put("weather_Details", mWeather_Details2);// 传入天气详情控制图片以及详情
map.put("weather_data", "2014年3月6日");// 控制日期
map.put("weather", mWeather_Info2);// 控制温度
map.put("weather_day", "星期四");// 控制星期
listData.add(map);
map = new HashMap<String, Object>();
map.put("weather_Details", mWeather_Details2);// 传入天气详情控制图片以及详情
map.put("weather_data", "2014年3月7日");// 控制日期
map.put("weather", mWeather_Info2);// 控制温度
map.put("weather_day", "星期五");// 控制星期
listData.add(map);
map = new HashMap<String, Object>();
map.put("weather_Details", mWeather_Details2);// 传入天气详情控制图片以及详情
map.put("weather_data", "2014年3月8日");// 控制日期
map.put("weather", mWeather_Info2);// 控制温度
map.put("weather_day", "星期六");// 控制星期
listData.add(map);
map = new HashMap<String, Object>();
map.put("weather_Details", mWeather_Details2);// 传入天气详情控制图片以及详情
map.put("weather_data", "2014年3月9日");// 控制日期
map.put("weather", mWeather_Info2);// 控制温度
map.put("weather_day", "星期日");// 控制星期
listData.add(map);
message = message.obtain();
message.obj = listData;
message.what = Constants.MSG_WHAT_PRESS_WEATHER;
// 发送消息
handler.sendMessage(message);
} catch (JSONException e) {
System.out.println("数据解析异常!");
} };
}.start(); }

json解析,总的来说就是简单数据。