Json Go openweathermap预测返回类型
我是新手,我正在尝试使用OpenWeatherMap构建一个小天气应用程序 还有briandowns的go套餐 我看天气预报没有问题 但我无法处理预测方法的结果Json Go openweathermap预测返回类型,json,go,openweathermap,Json,Go,Openweathermap,我是新手,我正在尝试使用OpenWeatherMap构建一个小天气应用程序 还有briandowns的go套餐 我看天气预报没有问题 但我无法处理预测方法的结果 func main() { apiKey := "XXXX" w, err := owm.NewForecast("5", "C", "en", apiKey) if err != nil { log.Fatal(err) } w.DailyByName("London", 1)
func main() {
apiKey := "XXXX"
w, err := owm.NewForecast("5", "C", "en", apiKey)
if err != nil {
log.Fatal(err)
}
w.DailyByName("London", 1)
data := w.ForecastWeatherJson
fmt.Println(data)
}
apiKey需要替换为有效密钥(注册后可以免费获得)
我的问题是从预测中提取信息。
其定义如下:
type ForecastWeatherJson interface {
Decode(r io.Reader) error
}
func (f *Forecast5WeatherData) Decode(r io.Reader) error {
if err := json.NewDecoder(r).Decode(&f); err != nil {
return err
}
return nil
}
在forecast.go文件中
解码定义为:
type ForecastWeatherJson interface {
Decode(r io.Reader) error
}
func (f *Forecast5WeatherData) Decode(r io.Reader) error {
if err := json.NewDecoder(r).Decode(&f); err != nil {
return err
}
return nil
}
在船艏楼5.5去
我真的不知道从哪里开始,因为我没有找到一个文档化的示例,该示例显示除其他语言外的数据处理(因此我想这是一个go特定的问题)。
我看到了如何在python中实现这一点,但在go情况下,返回类型对我来说并不清楚
欢迎提供任何示例提示或链接。您需要的数据已在
w
param中解码,但您需要键入assert以更正天气类型。在您的情况下,因为您使用的是type=5
,所以应该使用owm.Forecast5WeatherData
。然后你的主屏幕会像这样
func main() {
apiKey := "XXXX"
w, err := owm.NewForecast("5", "C", "en", apiKey)
if err != nil {
log.Fatal(err)
}
w.DailyByName("London", 3)
if val, ok := w.ForecastWeatherJson.(*owm.Forecast5WeatherData); ok {
fmt.Println(val)
fmt.Println(val.City)
fmt.Println(val.Cnt)
}
}