JAVA解析百度天气JSON字符串

以解析百度天气为例,使用json包

public static void main(String[] args) throws IOException {
        String ak = "自己的AK";
        String getURL = "http://api.map.baidu.com/telematics/v3/weather?output=json&location=西安&ak="+ak;
        //解决中文传输乱码
        getURL = new String(getURL.getBytes("UTF-8"));
	URL url = new URL(getURL);
	HttpURLConnection connect = (HttpURLConnection) url.openConnection(); 
	connect.connect();
	BufferedReader reader = new BufferedReader(new InputStreamReader(
	               connect.getInputStream(),"utf-8"));
	String lines;
	StringBuffer temp = new StringBuffer();
        while ((lines = reader.readLine()) != null) {
        	temp.append(lines);
        }
        reader.close();
        connect.disconnect();<pre name="code" class="java"><span style="white-space:pre">	</span>System.out.println("原始JSON数据:"+temp.toString());
//将回传的数据封装为json字符串 JSONObject jo = JSONObject.fromObject(temp.toString()); //回传给服务器的字符串 StringBuffer sb = new StringBuffer(); //拿到result数据并处理封装为json数组 JSONArray results = jo.getJSONArray("results"); sb.append("城市:"+results.getJSONObject(0).get("currentCity")).append("\n\n"); sb.append("pm2.5:"+results.getJSONObject(0).get("pm25")).append("\n\n"); //拿到result中index数据并处理封装为json数组 JSONArray index = results.getJSONObject(0).getJSONArray("index"); sb.append("穿衣指数:"+index.getJSONObject(0).get("des")).append("\n\n"); sb.append("洗车指数:"+index.getJSONObject(1).get("des")).append("\n\n"); sb.append("旅行指数:"+index.getJSONObject(2).get("des")).append("\n\n"); sb.append("感冒指数:"+index.getJSONObject(3).get("des")).append("\n\n"); sb.append("运动指数:"+index.getJSONObject(4).get("des")).append("\n\n"); sb.append("紫外线强度:"+index.getJSONObject(5).get("des")).append("\n\n"); //拿到result中weather_data数据并处理封装为json数组 JSONArray weather_data = results.getJSONObject(0).getJSONArray("weather_data");sb.append("------未来四天天气预报------").append("\n");//添加第一天 sb.append(weather_data.getJSONObject(0).get("date"));//日期 sb.append(":"+weather_data.getJSONObject(0).get("weather"));//天气 sb.append(","+weather_data.getJSONObject(0).get("wind"));//风力 sb.append(","+weather_data.getJSONObject(0).get("temperature")).append("\n");//温度 //添加第二天 sb.append(weather_data.getJSONObject(1).get("date"));//日期 sb.append(":"+weather_data.getJSONObject(1).get("weather"));//天气 sb.append(","+weather_data.getJSONObject(1).get("wind"));//风力 sb.append(","+weather_data.getJSONObject(1).get("temperature")).append("\n");//温度 //添加第三天 sb.append(weather_data.getJSONObject(2).get("date"));//日期 sb.append(":"+weather_data.getJSONObject(2).get("weather"));//天气 sb.append(","+weather_data.getJSONObject(2).get("wind"));//风力 sb.append(","+weather_data.getJSONObject(2).get("temperature")).append("\n");//温度 //添加第四天 sb.append(weather_data.getJSONObject(3).get("date"));//日期 sb.append(":"+weather_data.getJSONObject(3).get("weather"));//天气 sb.append(","+weather_data.getJSONObject(3).get("wind"));//风力 sb.append(","+weather_data.getJSONObject(3).get("temperature")).append("\n");//温度 System.out.println(sb.toString());}
 

执行结果图:



原始数据较长,截屏无法显示,下面文字显示:

原始JSON数据:{"error":0,"status":"success","date":"2015-12-03","results":[{"currentCity":"西安","pm25":"66","index":[{"title":"穿衣","zs":"较冷","tipt":"穿衣指数","des":"建议着厚外套加毛衣等服装。年老体弱者宜着大衣、呢外套加羊毛衫。"},{"title":"洗车","zs":"较适宜","tipt":"洗车指数","des":"较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。"},{"title":"旅游","zs":"适宜","tipt":"旅游指数","des":"天气较好,气温稍低,会感觉稍微有点凉,不过也是个好天气哦。适宜旅游,可不要错过机会呦!"},{"title":"感冒","zs":"易发","tipt":"感冒指数","des":"相对于今天将会出现大幅度降温,易发生感冒,请注意适当增加衣服,加强自我防护避免感冒。"},{"title":"运动","zs":"较不宜","tipt":"运动指数","des":"天气较好,但考虑天气寒冷,推荐您进行室内运动,户外运动时请注意保暖并做好准备活动。"},{"title":"紫外线强度","zs":"中等","tipt":"紫外线强度指数","des":"属中等强度紫外线辐射天气,外出时建议涂擦SPF高于15、PA+的防晒护肤品,戴帽子、太阳镜。"}],"weather_data":[{"date":"周四 12月03日 (实时:5℃)","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"晴","wind":"东北风微风","temperature":"9 ~ -1℃"},{"date":"周五","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/duoyun.png","weather":"多云","wind":"东北风微风","temperature":"8 ~ -1℃"},{"date":"周六","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/duoyun.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"多云转晴","wind":"东北风微风","temperature":"7 ~ 0℃"},{"date":"周日","dayPictureUrl":"http://api.map.baidu.com/images/weather/day/qing.png","nightPictureUrl":"http://api.map.baidu.com/images/weather/night/qing.png","weather":"晴","wind":"西北风微风","temperature":"10 ~ 1℃"}]}]}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值