java解析json天气api_java解析新浪天气接口json的例子

新浪天气是返回的json数据我们可以通过很多方式来处理,如java,php或js来直接解析新浪天气返回的数据了,今天我们就一起来看一些解析新浪天气接口json的例子

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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值