教程集 www.jiaochengji.com
教程集 >  脚本编程  >  java  >  正文 java解析新浪天气接口json的例子

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

发布时间:2016-10-15   编辑:jiaochengji.com
教程集为您提供java解析新浪天气接口json的例子等资源,欢迎您收藏本站,我们将为您提供最新的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"));
 
   }
  
 }
 
}

 

您可能感兴趣的文章:
java解析新浪天气接口json的例子
PHP百度天气预报小偷程序
php和java差在哪里?
php使用新浪微博API开发用户授权功能
PHP调用百度天气接口API实现查询实时天气
基于JQuery的访问WebService的代码(可访问Java[Xfire])
php ajax实现无刷新获取天气状态
Illustrator设计一个热气球制作教程
Illustrator简单绘制热气球实例教程分享
php新浪接口查询ip地理位置

[关闭]
~ ~