fastjson天气预报json数据解析

本文介绍如何使用FastJSON解析第三方天气API返回的JSON数据,包括天气预报和实况天气信息,涵盖获取一周天气详情的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fastjson天气预报json数据解析

核心的两个包

		<dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.68</version>
        </dependency>

第三方天气接口

使用 haoservice 可以免费申请测试次数

得到的json数据格式:

返回字段:
名称	类型	说明
sk	Object	当前实况天气
      temp	String	当前温度
      wind_direction	String	当前风向
      wind_strength	String	当前风力
      humidity	String	当前湿度
      time	String	更新时间
today	String	今天天气情况
      city	String	城市名称
      date_y	String	查询日前
      week	String	星期
      temperature	String	今日温度
      weather	String	今日天气
      fa	String	天气标识00:晴
      fb	String	天气标识53:霾 如果fa不等于fb,说明是组合天气
      wind	String	风向及风力
      dressing_index	String	穿衣指数
      dressing_advice	String	穿衣建议
      uv_index	String	紫外线强度
      comfort_index	String	舒适度指数
      wash_index	String	洗车指数
      travel_index	String	旅游指数
      exercise_index	String	晨练指数
      drying_index	String	干燥指数
future	String	未来几天天气

这就是调用api返回的json数据,前期可以使用它来测试,不用每次去调用api

{"result":{"future":[{"date":"20200427","week":"星期一","temperature":"15~31","weather":"晴","fa":"00","fb":"00","wind":"持续无风向 微风"},{"date":"20200428","week":"星期二","temperature":"17~30","weather":"多云","fa":"01","fb":"02","wind":"持续无风向 微风"},{"date":"20200429","week":"星期三","temperature":"18~29","weather":"阴","fa":"02","fb":"07","wind":"持续无风向 微风"},{"date":"20200430","week":"星期四","temperature":"18~28","weather":"多云","fa":"01","fb":"01","wind":"持续无风向 微风"},{"date":"20200501","week":"星期五","temperature":"18~29","weather":"多云","fa":"01","fb":"01","wind":"持续无风向 微风"},{"date":"20200502","week":"星期六","temperature":"19~30","weather":"晴","fa":"00","fb":"01","wind":"持续无风向 微风"},{"date":"20200503","week":"星期日","temperature":"19~30","weather":"多云","fa":"01","fb":"00","wind":"持续无风向 微风"},{"date":"20200504","week":"星期一","temperature":"19~33","weather":"阴","fa":"02","fb":"07","wind":"东风 微风"},{"date":"20200505","week":"星期二","temperature":"19~31","weather":"小雨","fa":"07","fb":"07","wind":"南风 微风"},{"date":"20200506","week":"星期三","temperature":"20~31","weather":"阴","fa":"02","fb":"07","wind":"北风 微风"},{"date":"20200507","week":"星期四","temperature":"19~30","weather":"小雨","fa":"07","fb":"07","wind":"东风 微风"},{"date":"20200508","week":"星期五","temperature":"17~31","weather":"小雨","fa":"07","fb":"07","wind":"东北风 3-5级"},{"date":"20200509","week":"星期六","temperature":"16~27","weather":"小雨","fa":"07","fb":"02","wind":"东北风 微风"},{"date":"20200510","week":"星期日","temperature":"16~29","weather":"阴","fa":"02","fb":"02","wind":"南风 微风"},{"date":"20200511","week":"星期一","temperature":"18~27","weather":"小雨","fa":"07","fb":"07","wind":"东风 微风"}],"today":{"week":"星期一","city":"成都","dressing_index":"热","travel_index":"较适宜","wash_index":"较适宜","comfort_index":"--","exercise_index":"较适宜","dressing_advice":"天气热,建议着短裙、短裤、短薄外套、T恤等夏季服装。","uv_index":"很强","drying_index":"--","temperature":"15~28","weather":"多云","date_y":"2020年04月27日","fa":"01","fb":"01","wind":"持续无风向 微风"},"sk":{"temp":"32","humidity":"13","wind_direction":"东风","time":"19:31","wind_strength":"3级"}},"reason":"成功","error_code":0}

对应实体类

我的测试目标是去取得接下来一周每一天天气情况,封装到weather类中:
weather是根据json数据里每一天的数据的属性来定义的

@Data
public class Weather {
    public String	date;
    public String	week;
	public String	temperature;
	public String	weather;
	public String	fa;
	public String	fb;
	public String	wind;
}

核心功能代码:
注意:key是自己去申请的

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.elephant.pojo.Weather;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class WeatherJsonService {
	//最终得到的七天天气的weather的一个list
 	List<Weather> weathers = new ArrayList<Weather>();
    String key="weather api key";

    public List<Weather> getWeatherInfo(String cityName) throws IOException {
        //获取http客户端
        CloseableHttpClient client = HttpClients. createDefault();
        //通过httpget方式来实现我们的get请求
        HttpGet httpGet = new HttpGet( "http://apis.haoservice.com/weather?key="+key+"&cityname="+cityName);
        CloseableHttpResponse Response = client.execute(httpGet);

        HttpEntity entity = Response . getEntity();
        //通过EntityUtils 来将我们的数据转换成字符串
        String str = EntityUtils.toString(entity, "UTF-8");
        System.out.println(str);

        JSONObject json = JSON.parseObject(str);
        String name = json.getString("result");
        JSONObject json2 = JSON.parseObject(name);
        String future = json2.getString("future");
        JSONArray jsonArr = JSON.parseArray(future);
        for (int i = 0; i < 7; i++) {
            JSONObject jsonObj1 = jsonArr.getJSONObject(i);
            Weather weather = JSON.parseObject(jsonObj1.toJSONString(),Weather.class);
            weathers.add(weather);
        }

        return weathers;
    }
}

我的源码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值