I am trying to process a song using API.I have tried using a specific URL in http.get and further unmarshalling the data but the only element returned in the console is {}. Any help to send me in the right direction is appreciated.
Edit: here is some code. I have this in my main file.
var data [2]Data
if err != nil {
log.Fatal(err)
}
defer response.Body.Close()
if response.StatusCode != 200 {
log.Fatal("Didn't get 200")
}
rawData, err := ioutil.ReadAll(response.Body)
if err != nil {
log.Fatal(err)
}
json.Unmarshal(rawData, &data)
fmt.Println(data[0])
I have a struct created in a separate file for JSON tags/keys.
type Data struct {
SongID string `json:id`
Name string `json:name`
}
json.Unmarshalreturns an error value, you should check if it'sniland if it isn't it will tell you what the problem is which will also be the reason you're seeing{}.[2]SongDatadoes not seem to match the json structure that is returned from that url. You need to pass a type tojson.Unmarshalthat matches the json structurally.