-1
Once again, I on this site.
This time, I’m having a hard time parsing the API response I’m communicating with. The API in question is this one: https://api.brasil.io/v1/dataset/covid19/caso/data/? is_last=True&state=&city=&is_last=True
type Data struct {
Count int `json:"1"`
Next bool `json:"null"`
Previous bool `json:"null"`
Results []Cidades
}
type Cidades struct {
Name string `json:"city"`
Confirmed int `json:"confirmed"`
Deaths int `json:"deaths"`
}
func main() {
... código acima disso que faz a requisição com Headers ...
resp, err := client.Do(request)
if err != nil {
panic(err.Error())
}
body, readErr := ioutil.ReadAll(resp.Body)
if readErr != nil {
log.Fatal(readErr)
}
cidade := Cidades{}
jsonErr := json.Unmarshal(body, &cidade)
if jsonErr != nil {
log.Fatal(jsonErr)
}
fmt.Println(cidade.Name)
}
All authentication is done correctly (thanks to this beautiful site).
I’ve tried to parser only "Results", but I can’t.
How can I do, for example, to assign Deaths to one variable and Confirmed to another, for example? From what I read, the http.Requests
comes in byte, and when I try to parse for JSON, it returns only Results as string.
Thank you, it helped me understand the concept properly, again.
– Dasx