GSON框架下將百度天氣JSON數(shù)據(jù)轉(zhuǎn)JavaBean
前言
在當(dāng)今數(shù)字化時代,數(shù)據(jù)的高效處理與轉(zhuǎn)換已成為軟件開發(fā)中不可或缺的關(guān)鍵環(huán)節(jié)。JSON(JavaScript Object Notation)作為一種輕量級的數(shù)據(jù)交換格式,因其簡潔易讀、易于解析的特點,在眾多領(lǐng)域被廣泛應(yīng)用,尤其是在網(wǎng)絡(luò)數(shù)據(jù)交互中。而 JavaBean 作為 Java 編程中一種重要的組件模式,能夠?qū)?shù)據(jù)封裝為對象,方便進行操作和管理。將 JSON 數(shù)據(jù)轉(zhuǎn)換為 JavaBean,不僅能夠提升數(shù)據(jù)的可操作性,還能更好地實現(xiàn)面向?qū)ο蟮脑O(shè)計理念。在眾多的 JSON 解析庫中,GSON 以其強大的功能、簡潔的 API 和高效的性能脫穎而出,成為開發(fā)者處理 JSON 數(shù)據(jù)的首選工具之一。GSON 提供了簡單易用的方法,能夠輕松地將 JSON 字符串轉(zhuǎn)換為 Java 對象,同時也支持將 Java 對象序列化為 JSON 字符串。這種雙向轉(zhuǎn)換的能力,使得 GSON 在處理復(fù)雜的 JSON 數(shù)據(jù)結(jié)構(gòu)時表現(xiàn)出色,極大地簡化了開發(fā)流程。

本文將深入探討如何在 GSON 框架下,將百度天氣的 JSON 數(shù)據(jù)轉(zhuǎn)換為 JavaBean。首先,我們將詳細(xì)分析百度天氣 JSON 數(shù)據(jù)的結(jié)構(gòu)特點。百度天氣的 JSON 數(shù)據(jù)包含多個層次和復(fù)雜的數(shù)據(jù)字段,如城市信息、天氣狀況、溫度、風(fēng)力、空氣質(zhì)量等。了解這些數(shù)據(jù)的結(jié)構(gòu),是實現(xiàn)準(zhǔn)確轉(zhuǎn)換的前提。我們將通過實際的 JSON 數(shù)據(jù)樣例,逐步解析每個字段的含義和作用。其次我們將進入核心實戰(zhàn)環(huán)節(jié)。根據(jù)百度天氣 JSON 數(shù)據(jù)的結(jié)構(gòu),設(shè)計相應(yīng)的 JavaBean 類。這些 JavaBean 類將作為數(shù)據(jù)的載體,用于存儲和操作天氣數(shù)據(jù)。我們將詳細(xì)介紹如何根據(jù) JSON 字段定義 JavaBean 的屬性,以及如何通過 GSON 的注解和配置來實現(xiàn)精確的映射關(guān)系。通過具體的代碼實現(xiàn),展示如何將復(fù)雜的百度天氣 JSON 數(shù)據(jù)轉(zhuǎn)換為 JavaBean 對象,并處理可能出現(xiàn)的常見問題,如字段缺失、數(shù)據(jù)類型不匹配等。最后,我們將總結(jié) GSON 在處理百度天氣 JSON 數(shù)據(jù)時的最佳實踐和注意事項。通過實際案例的分析,分享如何優(yōu)化代碼結(jié)構(gòu)、提高轉(zhuǎn)換效率以及確保數(shù)據(jù)的準(zhǔn)確性和完整性。同時,我們還將探討如何利用轉(zhuǎn)換后的 JavaBean 數(shù)據(jù)進行進一步的業(yè)務(wù)邏輯開發(fā),例如數(shù)據(jù)展示、數(shù)據(jù)分析等,為讀者提供更全面的實戰(zhàn)指導(dǎo)。
通過本文的深入解析和實戰(zhàn)操作,讀者將能夠掌握 GSON 框架下將百度天氣 JSON 數(shù)據(jù)轉(zhuǎn)換為 JavaBean 的完整流程,從而在實際開發(fā)中更加高效地處理類似的 JSON 數(shù)據(jù)轉(zhuǎn)換任務(wù)。無論你是初學(xué)者還是有一定經(jīng)驗的開發(fā)者,本文都將為你提供有價值的參考和指導(dǎo),幫助你在 JSON 數(shù)據(jù)處理領(lǐng)域邁向更高的臺階。
一、百度天氣JSON
百度天氣作為國內(nèi)知名的天氣信息服務(wù)提供商,其提供的天氣數(shù)據(jù)接口以 JSON 格式返回豐富的天氣信息,包括實時天氣、未來幾天的天氣預(yù)報、空氣質(zhì)量等。這些數(shù)據(jù)對于開發(fā)天氣相關(guān)的應(yīng)用(如天氣查詢應(yīng)用、出行規(guī)劃應(yīng)用等)具有極高的價值。然而,原始的 JSON 數(shù)據(jù)格式并不便于直接在 Java 應(yīng)用中使用,這就需要我們將 JSON 數(shù)據(jù)轉(zhuǎn)換為 JavaBean,以便更好地進行數(shù)據(jù)處理和業(yè)務(wù)邏輯實現(xiàn)。為了更好的實現(xiàn)天氣的JSON到JavaBean的轉(zhuǎn)換,我們首先對百度天氣接口的請求參數(shù)、返回參數(shù)和屬性映射關(guān)系進行簡單介紹。
1、請求參數(shù)
為了方便第一次閱讀本文的朋友對百度的天氣接口也有一定的了解,本文首先對百度天氣的請求參數(shù)進行簡單介紹。如果在開發(fā)過程中已經(jīng)非常熟悉相關(guān)的接口,可以直接進入下一個小節(jié)的內(nèi)容。用戶可通過行政區(qū)劃代碼查詢實時天氣信息及未來5天天氣預(yù)報。
| 參數(shù)名稱 | 參數(shù)含義 | 默認(rèn)值 | 字段類型 | 必選 |
|---|---|---|---|---|
| district_id | 區(qū)縣的行政區(qū)劃編碼,和location二選一 | 無 | string | 否 |
| location | 經(jīng)緯度,經(jīng)度在前緯度在后,逗號分隔。支持類型:bd09mc/bd09ll/wgs84/gcj02。 | 無 | double | 否 |
| ak | 開發(fā)者密鑰,可在API控制臺申請獲得 | 無 | string | 是 |
| data_type | 請求數(shù)據(jù)類型。數(shù)據(jù)類型有:now/fc/index/alert/fc_hour/all,控制返回內(nèi)容 | 無 | string | 是 |
| output | 返回格式,目前支持json/xml | json | string | 否 |
| coordtype | 支持類型:wgs84/bd09ll/bd09mc/gcj02 | wgs84 | string | 否 |
注意:如果district_id和location同時傳,默認(rèn)以district_id為準(zhǔn);
2、返回參數(shù)
| 參數(shù)名 | 參數(shù)類型 | 描述信息 | 返回條件 | 異常值 |
|---|---|---|---|---|
| address | Object | 地理位置信息 | - | - |
| country | String | 國家名稱 | 始終返回 | - |
| province | String | 省份名稱 | 始終返回 | - |
| city | String | 城市名稱 | 始終返回 | - |
| name | String | 區(qū)縣名稱 | 始終返回 | - |
| id | String | 區(qū)縣id | 始終返回 | - |
| now | Object | 實況數(shù)據(jù) | - | - |
| temp | Int | 溫度(℃) | 始終返回 | 999999 |
| feels_like | Int | 體感溫度(℃) | data_type=now/all | 999999 |
| rh | Int | 相對濕度(%) | data_type=now/all | 999999 |
| wind_class | String | 風(fēng)力等級 | data_type=now/all | 暫無 |
| wind_dir | String | 風(fēng)向描述 | data_type=now/all | 暫無 |
| text | String | 天氣現(xiàn)象 參考天氣取值對照表 | data_type=now/all | 暫無 |
| prec_1h | Double | 1小時累計降水量(mm) | data_type=now/all | 999999 |
| clouds | Int | 云量(%) | data_type=now/all | 999999 |
| vis | Int | 能見度(m) | data_type=now/all | 999999 |
| aqi | Int | 空氣質(zhì)量指數(shù)數(shù)值 | data_type=now/all | 999999 |
| pm25 | Int | pm2.5濃度(μg/m3) | data_type=now/all | 999999 |
| pm10 | Int | pm10濃度(μg/m3) | data_type=now/all | 999999 |
| no2 | Int | 二氧化氮濃度(μg/m3) | data_type=now/all | 999999 |
| so2 | Int | 二氧化硫濃度(μg/m3) | data_type=now/all | 999999 |
| o3 | Int | 臭氧濃度(μg/m3) | data_type=now/all | 999999 |
| co | Double | 一氧化碳濃度(mg/m3) | data_type=now/all | 999999 |
| uptime | String | 數(shù)據(jù)更新時間,北京時間 | data_type=now/all | - |
| alert | ObjectArray | 氣象預(yù)警數(shù)據(jù) | - | - |
| type | String | 預(yù)警事件類型 參考 天氣取值對照表中的預(yù)警類型 | data_type=alert/all | 暫無 |
| level | String | 預(yù)警事件等級 | data_type=alert/all | 暫無 |
| title | String | 預(yù)警標(biāo)題 | data_type=alert/all | - |
| desc | String | 預(yù)警詳細(xì)提示信息 | data_type=alert/all | - |
| indexes | ObjectArray | 生活指數(shù)數(shù)據(jù) | - | - |
| name | String | 生活指數(shù)中文名稱 | data_type=index/all | 暫無 |
| brief | String | 生活指數(shù)概要說明 | data_type=index/all | 暫無 |
| detail | String | 生活指數(shù)詳細(xì)說明 | data_type=index/all | 暫無 |
| forecasts | ObjectArray | 預(yù)報數(shù)據(jù) | - | - |
| date | String | 日期,北京時區(qū) | data_type=fc/all | - |
| week | String | 星期,北京時區(qū) | data_type=fc/all | - |
| high | Int | 最高溫度(℃) | data_type=fc/all | 999999 |
| low | Int | 最低溫度(℃) | data_type=fc/all | 999999 |
| wc_day | String | 白天風(fēng)力 | data_type=fc/all | 暫無 |
| wc_night | String | 晚上風(fēng)力 | data_type=fc/all | 暫無 |
| wd_day | String | 白天風(fēng)向 | data_type=fc/all | 暫無 |
| wd_night | String | 晚上風(fēng)向 | data_type=fc/all | 暫無 |
| text_day | String | 白天天氣現(xiàn)象 參考天氣取值對照表 | data_type=fc/all | 暫無 |
| text_night | String | 晚上天氣現(xiàn)象 參考天氣取值對照表 | data_type=fc/all | 暫無 |
| wind_angle | Int | 風(fēng)向角度(°) | data_type=now/all 且 user_type=vip | 999999 |
| uvi | Int | 紫外線指數(shù) | data_type=now/all 且 user_type=vip | 999999 |
| pressure | Int | 氣壓(hPa) | data_type=now/all 且 user_type=vip | 999999 |
| dpt | Int | 露 點溫度(℃) | data_type=now/all 且 user_type=vip | 999999 |
除了未來幾天的天氣實況以外,未來24小時逐小時預(yù)報返回參數(shù):
| 參數(shù)名 | 參數(shù)類型 | 描述信息 | 返回條件 | 異常值 |
|---|---|---|---|---|
| forecast_hours | Object Array | 預(yù)報數(shù)據(jù) | - | - |
| text | String | 天氣現(xiàn)象 參考天氣取值對照表 | data_type=fc_hour/all | "暫無" |
| temp_fc | Int | 溫度(℃) | data_type=fc_hour/all | 999999 |
| wind_class | String | 風(fēng)力等級 | data_type=fc_hour/all | "暫無" |
| wind_dir | String | 風(fēng)向描述 | data_type=fc_hour/all | "暫無" |
| rh | Int | 相對濕度 | data_type=fc_hour/all | 999999 |
| prec_1h | Double | 1小時累計降水量(mm) | data_type=fc_hour/all | 999999 |
| clouds | Int | 云量(%) | data_type=fc_hour/all | 999999 |
| data_time | String | 數(shù)據(jù)時間 | data_type=fc_hour/all | 999999 |
| wind_angle | Int | 風(fēng)向角度(°) | data_type=fc_hour/all 且 user_type=vip | 999999 |
| pop | Int | 降水概率(%) | data_type=fc_hour/all 且 user_type=vip | 999999 |
| uvi | Int | 紫外線指數(shù) | data_type=fc_hour/all 且 user_type=vip | 999999 |
| pressure | Int | 氣壓(hPa) | data_type=fc_hour/all 且 user_type=vip | 999999 |
| dpt | Int | 露 點溫度(℃) | data_type=fc_hour/all 且 user_type=vip | 999999 |
在熟悉了請求參數(shù)和返回參數(shù)之后,我們來看一下通過接口實際返回的JSON數(shù)據(jù)。
3、屬性映射
關(guān)于如何使用公共的接口來封裝百度的調(diào)用key的內(nèi)容,在之前的博文內(nèi)容有所介紹,這里不進行贅述。這里將給出某區(qū)縣的返回結(jié)果:

通過返回的信息可以清晰的看出,百度的天氣接口返回當(dāng)前天氣實況、告警信息、生活指數(shù)、未來逐日天氣預(yù)報和未來24小時的逐小時天氣預(yù)報。而這個接口規(guī)范將支持將這個JSON反序列化成JavaBean。
二、GSON屬性映射實戰(zhàn)
本節(jié)將進入核心實戰(zhàn)環(huán)節(jié)。根據(jù)百度天氣 JSON 數(shù)據(jù)的結(jié)構(gòu),設(shè)計相應(yīng)的 JavaBean 類。這些 JavaBean 類將作為數(shù)據(jù)的載體,用于存儲和操作天氣數(shù)據(jù)。我們將詳細(xì)介紹如何根據(jù) JSON 字段定義 JavaBean 的屬性,以及如何通過 GSON 的注解和配置來實現(xiàn)精確的映射關(guān)系。通過具體的代碼實現(xiàn),展示如何將復(fù)雜的百度天氣 JSON 數(shù)據(jù)轉(zhuǎn)換為 JavaBean 對象,并處理可能出現(xiàn)的常見問題,如字段缺失、數(shù)據(jù)類型不匹配等。
1、類對象映射
為了實現(xiàn)將JSON字符串反序列化成JavaBean,我們需要定義一個跟JSON字符串想匹配的對象。其對應(yīng)的屬性名稱應(yīng)該較為相似,包括:狀態(tài)、天氣信息對象、響應(yīng)消息字符串。核心代碼如下:
package com.yelang.project.weather.domain;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@ToString
public class BdWeatherDTO implements Serializable {
private static final long serialVersionUID = -3963983158543661660L;
private int status;
private WeatherInfoDTO result;
private String message;
}注意這里最核心的對象是WeatherInfoDTO對象,直接存儲轉(zhuǎn)換對象信息。經(jīng)過前面的JSON與JavaBean的對應(yīng)關(guān)系可知,這個result對象包含著當(dāng)前天氣信息、預(yù)警信息、生活指數(shù)信息和逐日天氣預(yù)報和未來24小時的逐小時預(yù)報。關(guān)鍵代碼如下:
package com.yelang.project.weather.domain;
import java.io.Serializable;
import java.util.List;
import com.google.gson.annotations.SerializedName;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@ToString
public class WeatherInfoDTO implements Serializable{
private static final long serialVersionUID = 5849724792198940369L;
private WeatherNow weatherNow;//實時天氣
private List<WeatherAlerts> alerts;
private List<WeatherIndexes> indexes;
private List<WeatherForecasts> forecasts;
private List<WeatherForecastHours> forecastHours;
}篇幅有限,這里不能逐一展開,這里以生活指數(shù)的JavaBean的定義為例進行說明。其它的對象定義基本與接口返回的參數(shù)和說明基本一致,在此不一一進行列舉。
package com.yelang.project.weather.domain;
import java.io.Serializable;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;
@NoArgsConstructor
@AllArgsConstructor
@Setter
@Getter
@ToString
public class WeatherIndexes implements Serializable{
private static final long serialVersionUID = 498655771178931771L;
@TableId(value ="pk_id")
private Long pkId ;//主鍵
@TableField(value="weather_pk_id")
private Long weatherPkId;//實時天氣信息主鍵
private String name;//生活指數(shù)中文名稱
private String brief;//生活指數(shù)概要說明
private String detail;//生活指數(shù)詳細(xì)說明
}
將實際的屬性信息完全列出來之后,下面就可以實現(xiàn)根據(jù)JSON屬性進行對象轉(zhuǎn)換。
2、屬性字段映射
眾所周知在JavaBean的編寫過程當(dāng)中,我們的json的命名方式和Java不盡相同,兩者存在著較大的偏差,因此這就非常容易導(dǎo)致json的key值跟javabean的屬性名存在較大的不一致,那么此時又應(yīng)該如何處理呢?其實Gson框架本身就存在這樣的考慮。如果遇到兩者的屬性名稱不一致的情況,直接使用@SerializedName("feels_like")來設(shè)置即可,如當(dāng)前天氣信息對應(yīng)的JavaBean:

經(jīng)過以上的定義,其對應(yīng)的屬性值就能正常的進行賦值。需要注意的是,在使用@SerializedName這個注解時,傳入的名稱應(yīng)當(dāng)是json文件的key值。這樣才能實現(xiàn)針對性的轉(zhuǎn)換。
3、日期數(shù)據(jù)映射
與屬性字段同時并存的一種情況是,在百度的天氣接口中,我們定義了一個Timestamp的數(shù)據(jù)類型,而在json接口中可能只是一個字符串,那么如何直接將字符串轉(zhuǎn)換成Timestamp呢?這時候就沒有直接的注解直接使用,需要我們自己來擴展出一個適配器來進行處理。首先我們定義一個Timestamp的適配器對象,然后在適配器中定義不同的轉(zhuǎn)換器來實現(xiàn)時間戳的格式化,關(guān)鍵代碼如下:
package com.yelang.project.weather.adapter;
import com.google.gson.TypeAdapter;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
* - 自定義時間戳轉(zhuǎn)換器,將百度天氣接口的字符串轉(zhuǎn)為時間類型
* @author 夜郎king
*
*/
public class TimestampTypeAdapter extends TypeAdapter<Timestamp>{
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
@Override
public void write(JsonWriter out, Timestamp value) throws IOException {
if (value == null) {
out.nullValue();
} else {
out.value(dateFormat.format(value));
}
}
@Override
public Timestamp read(JsonReader in) throws IOException {
String timestampString = in.nextString();
try {
return new Timestamp(dateFormat.parse(timestampString).getTime());
} catch (ParseException e) {
throw new IOException("Failed to parse timestamp: " + timestampString, e);
}
}
}
定義好適配器之后,接下來我們需要在JavaBean的配置當(dāng)中來進行啟用,啟用的關(guān)鍵代碼如下,這里需要使用的注解是@JsonAdapter:
@JsonAdapter(TimestampTypeAdapter.class) private Timestamp uptime;//數(shù)據(jù)更新時間,北京時間
經(jīng)過以上幾步,基本就實現(xiàn)了基于GSON的json字符串與JavaBean的屬性映射設(shè)置。接下來就可以實現(xiàn)數(shù)據(jù)的實際轉(zhuǎn)換了。
三、天氣接口對象展示
這里我們將總結(jié) GSON 在處理百度天氣 JSON 數(shù)據(jù)時的最佳實踐和注意事項。通過實際案例的分析,分享如何優(yōu)化代碼結(jié)構(gòu)、提高轉(zhuǎn)換效率以及確保數(shù)據(jù)的準(zhǔn)確性和完整性。本節(jié)將通過實際的代碼運行來演示接口的調(diào)用和屬性信息的打印。
1、接口調(diào)用
在前面的例子中,我們對天氣的相關(guān)處理接口進行了詳細(xì)的代碼講解,那么如何集成百度的天氣查詢接口并進行調(diào)用呢?下面以某具體區(qū)縣為例,具體介紹如何記性接口的調(diào)用。
package com.yelang.project.unihttp;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.burukeyou.uniapi.http.core.response.HttpResponse;
import com.google.gson.Gson;
import com.yelang.project.thridinterface.BaiduWeatherApiServcie;
import com.yelang.project.weather.domain.BdWeatherDTO;
import com.yelang.project.weather.domain.WeatherInfoDTO;
import com.yelang.project.weather.service.IWeatherNowService;
@SpringBootTest
@RunWith(SpringRunner.class)
public class BaiduWeather2DBCase {
private static final String DATA_TYPE = "all";
@Autowired
private BaiduWeatherApiServcie baiduWeatherApiService;
@Autowired
private IWeatherNowService weatherService;
@Test
public void bdWeather2PG() {
String district_id = "430811";//表示具體的行政區(qū)劃代號
HttpResponse<String> result = baiduWeatherApiService.getWeather(district_id, DATA_TYPE);
System.out.println(result.getBodyResult());
Gson gson = new Gson();
BdWeatherDTO bdWeatherInfo = gson.fromJson(result.getBodyResult(), BdWeatherDTO.class);
WeatherInfoDTO bdResult = bdWeatherInfo.getResult();
System.out.println(bdResult.getWeatherNow());
System.out.println(bdResult.getAlerts());
System.out.println(bdResult.getIndexes());
System.out.println(bdResult.getForecasts());
System.out.println(bdResult.getForecastHours());
}
}
下一節(jié)我們將根據(jù)接口的實際調(diào)用結(jié)果來實現(xiàn)相關(guān)的信息打印。來看看我們的設(shè)置是否正確,返回的JavaBean是否符合預(yù)期。
2、Java屬性打印輸出
當(dāng)我們在IDE中實際運行上述的代碼時,可以清晰的在Eclipse的運行窗口中看到以下的輸出:

可以明顯的看到,上述已經(jīng)將JSON數(shù)據(jù)轉(zhuǎn)為了正常的JavaBean。來看一下控制臺的信息輸出如下:
在這里可以做一些時候的處理,比如格式化和解析等,做成插件似的......
rsp===>com.burukeyou.uniapi.http.core.response.UniHttpResponse@7828bc6b
{"status":0,"result":{"location":{"country":"中國","province":"湖南省","city":"張家界市","name":"武陵源","id":"430811"},"now":{"text":"多云","temp":29,"feels_like":31,"rh":81,"wind_class":"1級","wind_dir":"北風(fēng)","prec_1h":0.0,"clouds":75,"vis":18900,"aqi":36,"pm25":25,"pm10":30,"no2":6,"so2":5,"o3":50,"co":0.6,"wind_angle":10,"uvi":0,"pressure":978,"dpt":25,"uptime":"20250810225500"},"indexes":[{"name":"晨練指數(shù)","brief":"不宜","detail":"有降水,建議在室內(nèi)做適當(dāng)鍛煉。"},{"name":"洗車指數(shù)","brief":"不適宜","detail":"兩天內(nèi)有雨,雨水和泥水會弄臟愛車。"},{"name":"感冒指數(shù)","brief":"少發(fā)","detail":"感冒機率較低,避免長期處于空調(diào)屋中。"},{"name":"紫外線指數(shù)","brief":"最弱","detail":"輻射弱,涂擦SPF8-12防曬護膚品。"},{"name":"穿衣指數(shù)","brief":"熱","detail":"適合穿T恤、短薄外套等???季服裝。"},{"name":"運動指數(shù)","brief":"較不宜","detail":"有降水,推薦您在室內(nèi)進行休閑運動。"}],"alerts":[],"forecasts":[{"text_day":"小雨","text_night":"中雨","high":29,"low":24,"wc_day":"<3級","wd_day":"東風(fēng)","wc_night":"<3級","wd_night":"東風(fēng)","date":"2025-08-10","week":"星期日"},{"text_day":"小雨","text_night":"小雨","high":29,"low":23,"wc_day":"<3級","wd_day":"東風(fēng)","wc_night":"<3級","wd_night":"東風(fēng)","date":"2025-08-11","week":"星期一"},{"text_day":"小雨","text_night":"大雨","high":29,"low":23,"wc_day":"<3級","wd_day":"東北風(fēng)","wc_night":"<3級","wd_night":"西北風(fēng)","date":"2025-08-12","week":"星期二"},{"text_day":"小雨","text_night":"小雨","high":32,"low":24,"wc_day":"<3級","wd_day":"東風(fēng)","wc_night":"<3級","wd_night":"東風(fēng)","date":"2025-08-13","week":"星期三"},{"text_day":"小雨","text_night":"小雨","high":33,"low":24,"wc_day":"<3級","wd_day":"東風(fēng)","wc_night":"<3級","wd_night":"東北風(fēng)","date":"2025-08-14","week":"星期四"},{"text_day":"小雨","text_night":"多云","high":32,"low":23,"wc_day":"<3級","wd_day":"南風(fēng)","wc_night":"<3級","wd_night":"東南風(fēng)","date":"2025-08-15","week":"星期五"},{"text_day":"小雨","text_night":"晴","high":35,"low":25,"wc_day":"<3級","wd_day":"西風(fēng)","wc_night":"<3級","wd_night":"西北風(fēng)","date":"2025-08-16","week":"星期六"}],"forecast_hours":[{"text":"小雨","temp_fc":30,"wind_class":"<3級","wind_dir":"東風(fēng)","rh":93,"prec_1h":0.9,"clouds":99,"wind_angle":109,"pop":70,"uvi":0,"pressure":979,"dpt":28,"data_time":"2025-08-10 23:00:00"},{"text":"小雨","temp_fc":29,"wind_class":"<3級","wind_dir":"東風(fēng)","rh":93,"prec_1h":0.9,"clouds":99,"wind_angle":106,"pop":60,"uvi":0,"pressure":979,"dpt":28,"data_time":"2025-08-11 00:00:00"},{"text":"中雨","temp_fc":29,"wind_class":"<3級","wind_dir":"東風(fēng)","rh":93,"prec_1h":1.6,"clouds":99,"wind_angle":108,"pop":80,"uvi":0,"pressure":979,"dpt":28,"data_time":"2025-08-11 01:00:00"},{"text":"中雨","temp_fc":29,"wind_class":"<3級","wind_dir":"東風(fēng)","rh":94,"prec_1h":1.6,"clouds":100,"wind_angle":107,"pop":70,"uvi":0,"pressure":980,"dpt":27,"data_time":"2025-08-11 02:00:00"},{"text":"中雨","temp_fc":28,"wind_class":"3~4級","wind_dir":"東風(fēng)","rh":94,"prec_1h":1.6,"clouds":99,"wind_angle":99,"pop":80,"uvi":0,"pressure":979,"dpt":26,"data_time":"2025-08-11 03:00:00"},{"text":"小雨","temp_fc":27,"wind_class":"3~4級","wind_dir":"東風(fēng)","rh":94,"prec_1h":0.3,"clouds":99,"wind_angle":93,"pop":60,"uvi":0,"pressure":978,"dpt":25,"data_time":"2025-08-11 04:00:00"},{"text":"陰","temp_fc":26,"wind_class":"3~4級","wind_dir":"東風(fēng)","rh":94,"prec_1h":0.0,"clouds":99,"wind_angle":88,"pop":0,"uvi":0,"pressure":978,"dpt":24,"data_time":"2025-08-11 05:00:00"},{"text":"小雨","temp_fc":25,"wind_class":"3~4級","wind_dir":"東風(fēng)","rh":93,"prec_1h":0.4,"clouds":99,"wind_angle":85,"pop":60,"uvi":0,"pressure":978,"dpt":24,"data_time":"2025-08-11 06:00:00"},{"text":"中雨","temp_fc":24,"wind_class":"<3級","wind_dir":"東風(fēng)","rh":93,"prec_1h":1.6,"clouds":99,"wind_angle":81,"pop":80,"uvi":0,"pressure":979,"dpt":23,"data_time":"2025-08-11 07:00:00"},{"text":"中雨","temp_fc":24,"wind_class":"<3級","wind_dir":"東風(fēng)","rh":93,"prec_1h":1.6,"clouds":100,"wind_angle":74,"pop":80,"uvi":0,"pressure":980,"dpt":22,"data_time":"2025-08-11 08:00:00"},{"text":"中雨","temp_fc":24,"wind_class":"<3級","wind_dir":"東風(fēng)","rh":92,"prec_1h":1.6,"clouds":100,"wind_angle":72,"pop":80,"uvi":0,"pressure":979,"dpt":23,"data_time":"2025-08-11 09:00:00"},{"text":"小雨","temp_fc":25,"wind_class":"<3級","wind_dir":"東風(fēng)","rh":91,"prec_1h":0.6,"clouds":100,"wind_angle":70,"pop":70,"uvi":0,"pressure":979,"dpt":23,"data_time":"2025-08-11 10:00:00"},{"text":"小雨","temp_fc":26,"wind_class":"<3級","wind_dir":"東風(fēng)","rh":91,"prec_1h":0.3,"clouds":100,"wind_angle":68,"pop":60,"uvi":0,"pressure":979,"dpt":24,"data_time":"2025-08-11 11:00:00"},{"text":"小雨","temp_fc":26,"wind_class":"<3級","wind_dir":"東北風(fēng)","rh":91,"prec_1h":0.6,"clouds":99,"wind_angle":50,"pop":70,"uvi":0,"pressure":978,"dpt":25,"data_time":"2025-08-11 12:00:00"},{"text":"中雨","temp_fc":27,"wind_class":"<3級","wind_dir":"東北風(fēng)","rh":91,"prec_1h":1.6,"clouds":99,"wind_angle":37,"pop":80,"uvi":0,"pressure":978,"dpt":25,"data_time":"2025-08-11 13:00:00"},{"text":"中雨","temp_fc":28,"wind_class":"<3級","wind_dir":"東北風(fēng)","rh":91,"prec_1h":1.6,"clouds":99,"wind_angle":27,"pop":80,"uvi":0,"pressure":978,"dpt":26,"data_time":"2025-08-11 14:00:00"},{"text":"中雨","temp_fc":28,"wind_class":"3~4級","wind_dir":"東北風(fēng)","rh":90,"prec_1h":1.6,"clouds":99,"wind_angle":25,"pop":80,"uvi":0,"pressure":978,"dpt":26,"data_time":"2025-08-11 15:00:00"},{"text":"小雨","temp_fc":28,"wind_class":"3~4級","wind_dir":"東北風(fēng)","rh":89,"prec_1h":0.7,"clouds":99,"wind_angle":24,"pop":70,"uvi":0,"pressure":978,"dpt":26,"data_time":"2025-08-11 16:00:00"},{"text":"小雨","temp_fc":29,"wind_class":"3~4級","wind_dir":"東北風(fēng)","rh":89,"prec_1h":0.5,"clouds":99,"wind_angle":23,"pop":60,"uvi":0,"pressure":978,"dpt":26,"data_time":"2025-08-11 17:00:00"},{"text":"小雨","temp_fc":29,"wind_class":"<3級","wind_dir":"東北風(fēng)","rh":88,"prec_1h":0.4,"clouds":98,"wind_angle":28,"pop":70,"uvi":0,"pressure":978,"dpt":27,"data_time":"2025-08-11 18:00:00"},{"text":"小雨","temp_fc":29,"wind_class":"<3級","wind_dir":"東北風(fēng)","rh":87,"prec_1h":0.1,"clouds":98,"wind_angle":40,"pop":70,"uvi":0,"pressure":978,"dpt":27,"data_time":"2025-08-11 19:00:00"},{"text":"陰","temp_fc":30,"wind_class":"<3級","wind_dir":"東風(fēng)","rh":87,"prec_1h":0.0,"clouds":98,"wind_angle":110,"pop":0,"uvi":0,"pressure":979,"dpt":27,"data_time":"2025-08-11 20:00:00"},{"text":"陰","temp_fc":28,"wind_class":"<3級","wind_dir":"東北風(fēng)","rh":90,"prec_1h":0.0,"clouds":92,"wind_angle":57,"pop":0,"uvi":0,"pressure":979,"dpt":26,"data_time":"2025-08-11 21:00:00"},{"text":"陰","temp_fc":26,"wind_class":"3~4級","wind_dir":"東北風(fēng)","rh":93,"prec_1h":0.0,"clouds":86,"wind_angle":45,"pop":0,"uvi":0,"pressure":979,"dpt":24,"data_time":"2025-08-11 22:00:00"}]},"message":"success"}
WeatherNow(pkId=null, locationCode=null, temp=29, feelsLike=31, rh=81, windClass=1級, windDir=北風(fēng), text=多云, prec1h=0.0, clouds=75, vis=18900, aqi=36, pm25=25, pm10=30, no2=6, so2=5, o3=50, co=0.6, uptime=2025-08-10 22:55:00.0)
[]
[WeatherIndexes(pkId=null, weatherPkId=null, name=晨練指數(shù), brief=不宜, detail=有降水,建議在室內(nèi)做適當(dāng)鍛煉。), WeatherIndexes(pkId=null, weatherPkId=null, name=洗車指數(shù), brief=不適宜, detail=兩天內(nèi)有雨,雨水和泥水會弄臟愛車。), WeatherIndexes(pkId=null, weatherPkId=null, name=感冒指數(shù), brief=少發(fā), detail=感冒機率較低,避免長期處于空調(diào)屋中。), WeatherIndexes(pkId=null, weatherPkId=null, name=紫外線指數(shù), brief=最弱, detail=輻射弱,涂擦SPF8-12防曬護膚品。), WeatherIndexes(pkId=null, weatherPkId=null, name=穿衣指數(shù), brief=熱, detail=適合穿T恤、短薄外套等夏季服裝。), WeatherIndexes(pkId=null, weatherPkId=null, name=運動指數(shù), brief=較不宜, detail=有降水,推薦您在室內(nèi)進行休閑運動。)]
[WeatherForecasts(pkId=null, weatherPkId=null, date=Sun Aug 10 00:00:00 CST 2025, week=星期日, high=29, low=24, wcDay=<3級, wcNight=<3級, wdDay=東風(fēng), wdNight=東風(fēng), textDay=小雨, textNight=中雨), WeatherForecasts(pkId=null, weatherPkId=null, date=Mon Aug 11 00:00:00 CST 2025, week=星期一, high=29, low=23, wcDay=<3級, wcNight=<3級, wdDay=東風(fēng), wdNight=東風(fēng), textDay=小雨, textNight=小雨), WeatherForecasts(pkId=null, weatherPkId=null, date=Tue Aug 12 00:00:00 CST 2025, week=星期二, high=29, low=23, wcDay=<3級, wcNight=<3級, wdDay=東北風(fēng), wdNight=西北風(fēng), textDay=小雨, textNight=大雨), WeatherForecasts(pkId=null, weatherPkId=null, date=Wed Aug 13 00:00:00 CST 2025, week=星期三, high=32, low=24, wcDay=<3級, wcNight=<3級, wdDay=東風(fēng), wdNight=東風(fēng), textDay=小雨, textNight=小雨), WeatherForecasts(pkId=null, weatherPkId=null, date=Thu Aug 14 00:00:00 CST 2025, week=星期四, high=33, low=24, wcDay=<3級, wcNight=<3級, wdDay=東風(fēng), wdNight=東北風(fēng), textDay=小雨, textNight=小雨), WeatherForecasts(pkId=null, weatherPkId=null, date=Fri Aug 15 00:00:00 CST 2025, week=星期五, high=32, low=23, wcDay=<3級, wcNight=<3級, wdDay=南風(fēng), wdNight=東南風(fēng), textDay=小雨, textNight=多云), WeatherForecasts(pkId=null, weatherPkId=null, date=Sat Aug 16 00:00:00 CST 2025, week=星期六, high=35, low=25, wcDay=<3級, wcNight=<3級, wdDay=西風(fēng), wdNight=西北風(fēng), textDay=小雨, textNight=晴)]
[WeatherForecastHours(pkId=null, weatherPkId=null, text=小雨, tempFc=30, windClass=<3級, windDir=東風(fēng), rh=93, prec1h=0.9, clouds=99, dataTime=Sun Aug 10 23:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=小雨, tempFc=29, windClass=<3級, windDir=東風(fēng), rh=93, prec1h=0.9, clouds=99, dataTime=Mon Aug 11 00:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=中雨, tempFc=29, windClass=<3級, windDir=東風(fēng), rh=93, prec1h=1.6, clouds=99, dataTime=Mon Aug 11 01:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=中雨, tempFc=29, windClass=<3級, windDir=東風(fēng), rh=94, prec1h=1.6, clouds=100, dataTime=Mon Aug 11 02:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=中雨, tempFc=28, windClass=3~4級, windDir=東風(fēng), rh=94, prec1h=1.6, clouds=99, dataTime=Mon Aug 11 03:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=小雨, tempFc=27, windClass=3~4級, windDir=東風(fēng), rh=94, prec1h=0.3, clouds=99, dataTime=Mon Aug 11 04:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=陰, tempFc=26, windClass=3~4級, windDir=東風(fēng), rh=94, prec1h=0.0, clouds=99, dataTime=Mon Aug 11 05:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=小雨, tempFc=25, windClass=3~4級, windDir=東風(fēng), rh=93, prec1h=0.4, clouds=99, dataTime=Mon Aug 11 06:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=中雨, tempFc=24, windClass=<3級, windDir=東風(fēng), rh=93, prec1h=1.6, clouds=99, dataTime=Mon Aug 11 07:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=中雨, tempFc=24, windClass=<3級, windDir=東風(fēng), rh=93, prec1h=1.6, clouds=100, dataTime=Mon Aug 11 08:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=中雨, tempFc=24, windClass=<3級, windDir=東風(fēng), rh=92, prec1h=1.6, clouds=100, dataTime=Mon Aug 11 09:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=小雨, tempFc=25, windClass=<3級, windDir=東風(fēng), rh=91, prec1h=0.6, clouds=100, dataTime=Mon Aug 11 10:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=小雨, tempFc=26, windClass=<3級, windDir=東風(fēng), rh=91, prec1h=0.3, clouds=100, dataTime=Mon Aug 11 11:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=小雨, tempFc=26, windClass=<3級, windDir=東北風(fēng), rh=91, prec1h=0.6, clouds=99, dataTime=Mon Aug 11 12:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=中雨, tempFc=27, windClass=<3級, windDir=東北風(fēng), rh=91, prec1h=1.6, clouds=99, dataTime=Mon Aug 11 13:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=中雨, tempFc=28, windClass=<3級, windDir=東北風(fēng), rh=91, prec1h=1.6, clouds=99, dataTime=Mon Aug 11 14:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=中雨, tempFc=28, windClass=3~4級, windDir=東北風(fēng), rh=90, prec1h=1.6, clouds=99, dataTime=Mon Aug 11 15:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=小雨, tempFc=28, windClass=3~4級, windDir=東北風(fēng), rh=89, prec1h=0.7, clouds=99, dataTime=Mon Aug 11 16:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=小雨, tempFc=29, windClass=3~4級, windDir=東北風(fēng), rh=89, prec1h=0.5, clouds=99, dataTime=Mon Aug 11 17:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=小雨, tempFc=29, windClass=<3級, windDir=東北風(fēng), rh=88, prec1h=0.4, clouds=98, dataTime=Mon Aug 11 18:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=小雨, tempFc=29, windClass=<3級, windDir=東北風(fēng), rh=87, prec1h=0.1, clouds=98, dataTime=Mon Aug 11 19:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=陰, tempFc=30, windClass=<3級, windDir=東風(fēng), rh=87, prec1h=0.0, clouds=98, dataTime=Mon Aug 11 20:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=陰, tempFc=28, windClass=<3級, windDir=東北風(fēng), rh=90, prec1h=0.0, clouds=92, dataTime=Mon Aug 11 21:00:00 CST 2025), WeatherForecastHours(pkId=null, weatherPkId=null, text=陰, tempFc=26, windClass=3~4級, windDir=東北風(fēng), rh=93, prec1h=0.0, clouds=86, dataTime=Mon Aug 11 22:00:00 CST 2025)]
四、總結(jié)
通過本文的深入解析和實戰(zhàn)操作,讀者將能夠掌握 GSON 框架下將百度天氣 JSON 數(shù)據(jù)轉(zhuǎn)換為 JavaBean 的完整流程,從而在實際開發(fā)中更加高效地處理類似的 JSON 數(shù)據(jù)轉(zhuǎn)換任務(wù)。無論你是初學(xué)者還是有一定經(jīng)驗的開發(fā)者,本文都將為你提供有價值的參考和指導(dǎo),幫助你在 JSON 數(shù)據(jù)處理領(lǐng)域邁向更高的臺階。
以上就是GSON框架下將百度天氣JSON數(shù)據(jù)轉(zhuǎn)JavaBean的詳細(xì)內(nèi)容,更多關(guān)于JSON數(shù)據(jù)轉(zhuǎn)JavaBean的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
MyBatis中執(zhí)行相關(guān)SQL語句的方法
本文主要介紹了MyBatis中執(zhí)行相關(guān)SQL語句的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-08-08
Java 字節(jié)數(shù)組(byte[])和整型(int)的相互轉(zhuǎn)換
在Java編程中,有時需要將字節(jié)類型(byte)轉(zhuǎn)換為整數(shù)類型(int),或者反過來轉(zhuǎn)換,本文主要介紹了Java 字節(jié)數(shù)組(byte[])和整型(int)的相互轉(zhuǎn)換,感興趣的可以了解一下2023-12-12
SpringMVC @GetMapping注解路徑?jīng)_突問題解決
MD5對密碼進行加密存儲是常見的一種加密方式,本文主要介紹了Java雙重MD5加密實現(xiàn)安全登錄,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
基于Spring Security的Oauth2授權(quán)實現(xiàn)方法
這篇文章主要介紹了基于Spring Security的Oauth2授權(quán)實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

