I'm using the answer here to fetch some json from an api:
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
func main() {
resp, err := http.Get("http://api.openweathermap.org/data/2.5/forecast?id=524901&appid=1234")
if err != nil {
log.Fatal(err)
}
var generic map[string]interface{}
err = json.NewDecoder(resp.Body).Decode(&generic)
if err != nil {
log.Fatal(err)
}
fmt.Println(generic)
}
It's working, I have a map of the json stored in the generic
variable.
When I print with:
fmt.Println(generic["city"])
I get back:
map[id:524901 name:Moscow coord:map[lon:37.615555 lat:55.75222] country:RU population:0 sys:map[population:0]]
Now I want to access the city name. I'm trying the following:
fmt.Println(generic["city"]["name"])
And I get error:
muxTest/router.go:30: invalid operation: generic["city"]["name"] (type interface {} does not support indexing)
Also tried generic["city"].name
which didn't work.
How can I access the city name value?