作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我是新来的,我正在尝试使用 OpenWeatherMap 构建一个小天气应用程序和 briandowns 的 go-package。
我对读取当前天气没问题但我无法处理预测方法的结果。
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 替换为有效的(注册后可免费获得)。
我的问题是从 ForecastWeatherJson 中提取信息。它被定义为:
type ForecastWeatherJson interface {
Decode(r io.Reader) error
}
在 forecast.go 文件中。
解码定义为:
func (f *Forecast5WeatherData) Decode(r io.Reader) error {
if err := json.NewDecoder(r).Decode(&f); err != nil {
return err
}
return nil
}
在 forecast5.go 中。
我真的不知道从哪里开始,因为我没有找到显示处理除其他语言之外的数据的文档示例(所以我想这是一个特定于 go 的问题)。我看到了它是如何完成的。 python 但在 go 的情况下,返回类型对我来说不清楚。
感谢任何提示或示例链接。
最佳答案
您需要的数据已经在您的 w
参数中解码,但您需要键入 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)
}
}
关于json - Go openweathermap预报返回类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54893668/
我是一名优秀的程序员,十分优秀!