Repository navigation
Expand file tree
/
Copy pathWeatherReport.go
More file actions
93 lines (81 loc) · 2.11 KB
/
Copy pathWeatherReport.go
File metadata and controls
93 lines (81 loc) · 2.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// weatherreaport.go
// This file was generated from JSON Schema using quicktype, do not modify it directly.
// To parse and unparse this JSON data, add this code to your project and do:
//
// weatherReaport, err := UnmarshalWeatherReaport(bytes)
// bytes, err = weatherReaport.Marshal()
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
)
func UnmarshalWeatherReaport(data []byte) (WeatherReaport, error) {
var r WeatherReaport
err := json.Unmarshal(data, &r)
if err != nil {
fmt.Printf("Unmarshal err=%v\n", err)
}
var degree1 = string(*r.Data.Observe.Degree)
fmt.Print("温度:")
fmt.Println(degree1)
var weather1 = string(*r.Data.Observe.Weather)
fmt.Print("天气:")
fmt.Println(weather1)
return r, err
}
func (r *WeatherReaport) Marshal() ([]byte, error) {
return json.Marshal(r)
}
type WeatherReaport struct {
Data *Data `json:"data,omitempty"`
Message *string `json:"message,omitempty"`
Status *int64 `json:"status,omitempty"`
}
/*
{
"data":{
"observe":{
"degree":"26",
"humidity":"80",
"precipitation":"0",
"pressure":"1005",
"update_time":"202209151900",
"weather":"多云",
"weather_code":"01",
"weather_short":"多云",
"wind_direction":"1",
"wind_power":"1"
}
},
"message":"OK",
"status":200
}
*/
func main() {
var province, city string
fmt.Printf("请输入省份(中文):\n\t")
fmt.Scan(&province)
fmt.Printf("请输入城市(中文):\n\t")
fmt.Scan(&city)
fmt.Printf(province + "\t" + city + "\n")
fmt.Println()
url := "https://wis.qq.com/weather/common?source=pc&city=" + city + "&province=" + province + "&weather_type=observe"
//url := "https://wis.qq.com/weather/common?source=pc&city=宁波&province=浙江&weather_type=observe"
response, err := http.Get(url)
if err != nil {
log.Println(err)
return
}
defer response.Body.Close()
// fmt.Println()
result, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Println(err)
return
}
//fmt.Println(string(result))
UnmarshalWeatherReaport(result)
}