java 新浪

编程入门 行业动态 更新时间:2024-10-07 14:27:25

java <a href=https://www.elefans.com/category/jswz/34/1766670.html style=新浪"/>

java 新浪

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": ".png",

"nightPictureUrl": ".png",

"weather": "阵雨转多云",

"wind": "微风",

"temperature": "19 ~ 15℃"

},

{

"date": "周二",

"dayPictureUrl": ".png",

"nightPictureUrl": ".png",

"weather": "多云",

"wind": "微风",

"temperature": "22 ~ 16℃"

},

{

"date": "周三",

"dayPictureUrl": ".png",

"nightPictureUrl": ".png",

"weather": "多云",

"wind": "微风",

"temperature": "24 ~ 16℃"

},

{

"date": "周四",

"dayPictureUrl": ".png",

"nightPictureUrl": ".png",

"weather": "多云转阵雨",

"wind": "微风",

"temperature": "19 ~ 12℃"

}

]

}

]

代码如下

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

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import java.MalformedURLException;

import java.URL;

import java.URLConnection;

import java.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 = "=厦门&output=json&ak=W69oaDTCfuGwzNwmtVvgWfGH";

StringBuffer strBuf;

try {

baiduUrl = "="

+ 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"));

}

}

}

更多推荐

java 新浪

本文发布于:2024-02-06 20:37:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1751150.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:新浪   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!