In Python you can do something like this:
r = requests.get("http://wikidata.org/w/api.php", params=params)
data = r.json()
And now data
is a dict or hash table (also, I did not need to define beforehand the structure of the dict), and I can access values of keys by doing data["entities"], data["entities"]["Q12"], etc.
How would I do this in golang? So far I have this:
resp, err := http.Get("http://wikidata.org/w/api.php?"+v.Encode())
if err != nil {
// handle error
}
defer resp.Body.Close()
decoder := json.NewDecoder(resp.Body)
var data interface{}
decodeErr := decoder.Decode(&data)
if decodeErr != nil {
// handle error
}
fmt.Println(data["entities"], data["entities"]["Q"+id])
Which gives me the compile error: invalid operation: data["entities"] (index of type interface {})
So what type should var data
be? And do I need to define a structure to the JSON beforehand or is it possible to handle any JSON file/stream without modifying the code?