程序员一代码搬运工
【Java学习心得和Java学习技术个人总结】

java解析新浪天气接口json

json如下

[
    {
        "currentCity": "厦门", 
        "pm25": "64", 
        "index": [
            {
                "title": "穿衣", 
                "zs": "较舒适", 
                "tipt": "穿衣指数", 
                "des": "建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。"
            }, 
            {
                "title": "洗车", 
                "zs": "不宜", 
                "tipt": "洗车指数", 
                "des": "不宜洗车,未来24小时内有雨,如果在此期间洗车,雨水和路上的泥水可能会再次弄脏您的爱车。"
            }, 
            {
                "title": "旅游", 
                "zs": "适宜", 
                "tipt": "旅游指数", 
                "des": "天气较好,温度适宜,总体来说还是好天气哦,这样的天气适宜旅游,您可以尽情地享受大自然的风光。"
            }, 
            {
                "title": "感冒", 
                "zs": "少发", 
                "tipt": "感冒指数", 
                "des": "各项气象条件适宜,无明显降温过程,发生感冒机率较低。"
            }, 
            {
                "title": "运动", 
                "zs": "较适宜", 
                "tipt": "运动指数", 
                "des": "阴天,较适宜进行各种户内外运动。"
            }, 
            {
                "title": "紫外线强度", 
                "zs": "最弱", 
                "tipt": "紫外线强度指数", 
                "des": "属弱紫外线辐射天气,无需特别防护。若长期在户外,建议涂擦SPF在8-12之间的防晒护肤品。"
            }
        ], 
        "weather_data": [
            {
                "date": "周一 12月21日 (实时:16℃)", 
                "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/zhenyu.png", 
                "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/duoyun.png", 
                "weather": "阵雨转多云", 
                "wind": "微风", 
                "temperature": "19 ~ 15℃"
            }, 
            {
                "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": "22 ~ 16℃"
            }, 
            {
                "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": "24 ~ 16℃"
            }, 
            {
                "date": "周四", 
                "dayPictureUrl": "http://api.map.baidu.com/images/weather/day/duoyun.png", 
                "nightPictureUrl": "http://api.map.baidu.com/images/weather/night/zhenyu.png", 
                "weather": "多云转阵雨", 
                "wind": "微风", 
                "temperature": "19 ~ 12℃"
            }
        ]
    }
]

 

代码如下

因为天气接口一般是不会变的 所以简单的给他解析下就行了。。

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import org.apache.tools.ant.types.CommandlineJava.SysProperties;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class BaiduWeatherApi {

	public static String getWeatherInform(String cityName) {

	
		String baiduUrl = "http://api.map.baidu.com/telematics/v3/weather?location=厦门&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH";
		StringBuffer strBuf;

		try {

			baiduUrl = "http://api.map.baidu.com/telematics/v3/weather?location="
					+ URLEncoder.encode(cityName, "utf-8")
					+ "&output=json&ak=57ab4d7f7882e2028de5a9a589ae697f";
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}

		strBuf = new StringBuffer();

		try {
			URL url = new URL(baiduUrl);
			URLConnection conn = url.openConnection();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					conn.getInputStream(), "utf-8"));// ת�롣
			String line = null;
			while ((line = reader.readLine()) != null)
				strBuf.append(line + " ");
			reader.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return strBuf.toString();
	}
	
	
	public static void main(String[] args) {
	
		String strPar=BaiduWeatherApi.getWeatherInform("厦门");
		 JSONObject dataOfJson = JSONObject.fromObject(strPar);
		 System.out.println(dataOfJson);
		 if(dataOfJson.getInt("error")!=0){
			 System.out.println("没有获取到数据");
		 }else{
			
			 System.out.println("日期:"+dataOfJson.getString("date"));
			// ;
			 System.out.println(dataOfJson.getString("results"));
			 JSONArray array  =JSONArray.fromObject(dataOfJson.getString("results"));
			 JSONObject data = JSONObject.fromObject(array.get(0).toString());
			 System.out.println("城市:"+data.getString("currentCity"));
			 JSONArray arrayweather  =JSONArray.fromObject(data.getString("weather_data"));
			 
			 JSONObject weather = JSONObject.fromObject(arrayweather.get(0).toString());
//			 "weather":"晴",
//             "wind":"东北风3-4级",
//             "temperature":"15 ~ 9℃"
			 System.out.println("天气:"+weather.getString("weather"));
			 System.out.println("风向:"+ weather.getString("wind"));
			 System.out.println("温度:"+ weather.getString("temperature"));

		 }
		
	}

}

 

赞(0)
未经允许不得转载:程序员一代码搬运工 » java解析新浪天气接口json
分享到: 更多 (0)