commit f811538bdc70090467b34959beb7580dca5f1ead Author: zyx <1029606625@qq.com> Date: Fri Nov 4 20:07:12 2022 +0800 2022/11/04 20:05 上传第9次作业题2 diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..35410ca --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# 默认忽略的文件 +/shelf/ +/workspace.xml +# 基于编辑器的 HTTP 客户端请求 +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 0000000..0f55251 --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000..aa00ffa --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/.idea/jarRepositories.xml b/.idea/jarRepositories.xml new file mode 100644 index 0000000..abb532a --- /dev/null +++ b/.idea/jarRepositories.xml @@ -0,0 +1,20 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..132404b --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,14 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..9f5411f --- /dev/null +++ b/pom.xml @@ -0,0 +1,30 @@ + + + 4.0.0 + + cn.czyx007.week10 + week10-work2 + 1.0-SNAPSHOT + + + 8 + 8 + UTF-8 + + + + + com.google.code.gson + gson + 2.10 + + + org.projectlombok + lombok + 1.18.20 + + + + \ No newline at end of file diff --git a/src/main/java/cn/czyx007/week10/Driver.java b/src/main/java/cn/czyx007/week10/Driver.java new file mode 100644 index 0000000..cc5e927 --- /dev/null +++ b/src/main/java/cn/czyx007/week10/Driver.java @@ -0,0 +1,46 @@ +package cn.czyx007.week10; + +import cn.czyx007.week10.bean.*; +import com.google.gson.Gson; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.util.List; + +/** + * @author : 张宇轩 + * @createTime : 2022/11/4 - 18:53 + */ +public class Driver { + public static void main(String[] args) { + try(BufferedReader br = new BufferedReader(new FileReader("src/main/resources/weather.json"))) { + StringBuilder jsonStr = new StringBuilder(); + String aLine; + while ((aLine= br.readLine()) != null){ + jsonStr.append(aLine); + } + + Response response = new Gson().fromJson(jsonStr.toString(), Response.class); + Result result = response.getResult(); + Sk sk = result.getSk(); + Today today = result.getToday(); + List future = result.getFuture(); + +// System.out.println(response); +// System.out.println(sk); +// System.out.println(today); +// future.forEach(System.out::println); + + System.out.println("当前温度:" + sk.getTemp()); + System.out.println("今日天气:" + today.getWeather()); + System.out.println("穿衣指数:" + today.getDressing_index()); + int dayCnt = 2; + FutureDay futureDay = future.get(dayCnt - 1); + System.out.println("未来的第二天的温度:" + futureDay.getTemperature()); + System.out.println("未来的第二天的天气:" + futureDay.getWeather()); + + } catch (Exception e){ + e.printStackTrace(); + } + } +} diff --git a/src/main/java/cn/czyx007/week10/bean/FutureDay.java b/src/main/java/cn/czyx007/week10/bean/FutureDay.java new file mode 100644 index 0000000..765c17d --- /dev/null +++ b/src/main/java/cn/czyx007/week10/bean/FutureDay.java @@ -0,0 +1,54 @@ +package cn.czyx007.week10.bean; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Map; +import java.util.Set; + +/** + * @author : 张宇轩 + * @createTime : 2022/11/4 + */ +@NoArgsConstructor +@AllArgsConstructor +@Data +public class FutureDay { + private String temperature; + private String weather; + private Map weather_id; + private String wind; + private String week; + private String date; + + public String displayWeatherId(){ + StringBuilder weatherIdStr = new StringBuilder(); + weatherIdStr.append("\t\t\t\t\"weather_id\": {\n"); + Set keys = weather_id.keySet(); + int size = keys.size(); + int cnt = 0; + for (String key : keys) { + weatherIdStr.append("\t\t\t\t\t\"").append(key).append("\": "); + weatherIdStr.append("\"").append(weather_id.get(key)).append("\""); + if (cnt++ < size-1){ + weatherIdStr.append(","); + } + weatherIdStr.append("\n"); + } + weatherIdStr.append("\t\t\t\t},\n"); + return weatherIdStr.toString(); + } + + @Override + public String toString() { + return "\t\t\t{\n" + + "\t\t\t\t\"temperature\": " + "\"" + temperature + "\",\n" + + "\t\t\t\t\"weather\": " + "\"" + weather + "\",\n" + + displayWeatherId() + + "\t\t\t\t\"wind\": " + "\"" + wind + "\",\n" + + "\t\t\t\t\"week\": " + "\"" + week + "\",\n" + + "\t\t\t\t\"date\": " + "\"" + date + "\"\n" + + "\t\t\t}"; + } +} diff --git a/src/main/java/cn/czyx007/week10/bean/Response.java b/src/main/java/cn/czyx007/week10/bean/Response.java new file mode 100644 index 0000000..de3f04c --- /dev/null +++ b/src/main/java/cn/czyx007/week10/bean/Response.java @@ -0,0 +1,29 @@ +package cn.czyx007.week10.bean; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @author : 张宇轩 + * @createTime : 2022/11/4 + */ +@NoArgsConstructor +@AllArgsConstructor +@Data +public class Response { + private String resultcode; + private String reason; + private Result result; + private Integer error_code; + + @Override + public String toString() { + return "{\n" + + "\t\"resultcode\": " + "\"" + resultcode + "\",\n" + + "\t\"reason\": " + "\"" + reason + "\",\n" + + result + + "\t\"error_code\": " + error_code + "\n" + + "}"; + } +} diff --git a/src/main/java/cn/czyx007/week10/bean/Result.java b/src/main/java/cn/czyx007/week10/bean/Result.java new file mode 100644 index 0000000..31940b7 --- /dev/null +++ b/src/main/java/cn/czyx007/week10/bean/Result.java @@ -0,0 +1,43 @@ +package cn.czyx007.week10.bean; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.List; + +/** + * @author : 张宇轩 + * @createTime : 2022/11/4 + */ +@NoArgsConstructor +@AllArgsConstructor +@Data +public class Result { + private Sk sk; + private Today today; + private List future; + + public String displayFuture(){ + StringBuilder futureStr = new StringBuilder(); + futureStr.append("\t\t\"future\": [\n"); + for (int i = 0; i < future.size(); i++) { + futureStr.append(future.get(i)); + if(i < future.size()-1){ + futureStr.append(","); + } + futureStr.append("\n"); + } + futureStr.append("\t\t]\n"); + return futureStr.toString(); + } + + @Override + public String toString() { + return "\t\"result\": {\n" + + sk + + today + + displayFuture() + + "\t},\n"; + } +} diff --git a/src/main/java/cn/czyx007/week10/bean/Sk.java b/src/main/java/cn/czyx007/week10/bean/Sk.java new file mode 100644 index 0000000..785071c --- /dev/null +++ b/src/main/java/cn/czyx007/week10/bean/Sk.java @@ -0,0 +1,31 @@ +package cn.czyx007.week10.bean; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +/** + * @author : 张宇轩 + * @createTime : 2022/11/4 + */ +@NoArgsConstructor +@AllArgsConstructor +@Data +public class Sk { + private String temp; + private String wind_direction; + private String wind_strength; + private String humidity; + private String time; + + @Override + public String toString() { + return "\t\t\"sk\": {\n" + + "\t\t\t\"temp\": " + "\"" + temp + "\",\n" + + "\t\t\t\"wind_direction\": " + "\"" + wind_direction + "\",\n" + + "\t\t\t\"wind_strength\": " + "\"" + wind_strength + "\",\n" + + "\t\t\t\"humidity\": " + "\"" + humidity + "\",\n" + + "\t\t\t\"time\": " + "\"" + time + "\"\n" + + "\t\t},\n"; + } +} diff --git a/src/main/java/cn/czyx007/week10/bean/Today.java b/src/main/java/cn/czyx007/week10/bean/Today.java new file mode 100644 index 0000000..2205244 --- /dev/null +++ b/src/main/java/cn/czyx007/week10/bean/Today.java @@ -0,0 +1,70 @@ +package cn.czyx007.week10.bean; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +import java.util.Map; +import java.util.Set; + +/** + * @author : 张宇轩 + * @createTime : 2022/11/4 - 18:40 + */ +@NoArgsConstructor +@AllArgsConstructor +@Data +public class Today { + private String city; + private String date_y; + private String week; + private String temperature; + private String weather; + private Map weather_id; + private String wind; + private String dressing_index; + private String dressing_advice; + private String uv_index; + private String wash_index; + private String travel_index; + private String exercise_index; + private String drying_index; + + public String displayWeatherId(){ + StringBuilder weatherIdStr = new StringBuilder(); + weatherIdStr.append("\t\t\t\"weather_id\": {\n"); + Set keys = weather_id.keySet(); + int size = keys.size(); + int cnt = 0; + for (String key : keys) { + weatherIdStr.append("\t\t\t\t\"").append(key).append("\": "); + weatherIdStr.append("\"").append(weather_id.get(key)).append("\""); + if (cnt++ < size-1){ + weatherIdStr.append(","); + } + weatherIdStr.append("\n"); + } + weatherIdStr.append("\t\t\t},\n"); + return weatherIdStr.toString(); + } + + @Override + public String toString() { + return "\t\t\"today\": {\n" + + "\t\t\t\"city\": " + "\"" + city + "\",\n" + + "\t\t\t\"date_y\": " + "\"" + date_y + "\",\n" + + "\t\t\t\"week\": " + "\"" + week + "\",\n" + + "\t\t\t\"temperature\": " + "\"" + temperature + "\",\n" + + "\t\t\t\"weather\": " + "\"" + weather + "\",\n" + + displayWeatherId() + + "\t\t\t\"wind\": " + "\"" + wind + "\",\n" + + "\t\t\t\"dressing_index\": " + "\"" + dressing_index + "\",\n" + + "\t\t\t\"dressing_advice\": " + "\"" + dressing_advice + "\",\n" + + "\t\t\t\"uv_index\": " + "\"" + uv_index + "\",\n" + + "\t\t\t\"wash_index\": " + "\"" + wash_index + "\",\n" + + "\t\t\t\"travel_index\": " + "\"" + travel_index + "\",\n" + + "\t\t\t\"exercise_index\": " + "\"" + exercise_index + "\",\n" + + "\t\t\t\"drying_index\": " + "\"" + drying_index + "\"\n" + + "\t\t},\n"; + } +} diff --git a/src/main/resources/weather.json b/src/main/resources/weather.json new file mode 100644 index 0000000..b8e4fdd --- /dev/null +++ b/src/main/resources/weather.json @@ -0,0 +1,57 @@ +{ + "resultcode": "200", + "reason": "查询成功!", + "result": { + "sk": { + "temp": "21", + "wind_direction": "西风", + "wind_strength": "2 级", + "humidity": "4%", + "time": "14:25" + }, + "today": { + "city": "武汉", + "date_y": "2014 年 03 月 21 日", + "week": "星期五", + "temperature": "8℃~20℃", + "weather": "晴转霾", + "weather_id": { + "fa": "00", + "fb": "53" + }, + "wind": "西南风微风", + "dressing_index": "较冷", + "dressing_advice": "建议着大衣、呢外套加毛衣、卫衣等服装。", + "uv_index": "中等", + "wash_index": "较适宜", + "travel_index": "适宜", + "exercise_index": "较适宜", + "drying_index": "" + }, + "future": [ + { + "temperature": "28℃~36℃", + "weather": "晴转多云", + "weather_id": { + "fa": "00", + "fb": "01" + }, + "wind": "南风 3-4 级", + "week": "星期一", + "date": "20140804" + }, + { + "temperature": "28℃~36℃", + "weather": "晴转多云", + "weather_id": { + "fa": "00", + "fb": "01" + }, + "wind": "东南风 3-4 级", + "week": "星期二", + "date": "20140805" + } + ] + }, + "error_code": 0 +}