So I’m learning ways to use json data, so for A) I have hardcoded the .json data and it works, but when I try to read the same data but from a .json file (B), it executes but nothing prints.
A)
text := "[{\"Id\": 100, \"Name\": \"Go\"}, {\"Id\": 200, \"Name\": \"Java\"}]"
bytes := []byte(text)
var boxes []Box
json.Unmarshal (bytes, &boxes)
for i := range boxes {
fmt.Printf("Id = %v, Name = %v", boxes[i].Id, boxes[i].Name)
fmt.Println()
}
/* Output:
Id = 100, Name = Go
Id = 200, Name = Java
*/
B)
bytes, _ := ioutil.ReadFile("file.json")
var boxes []Box
json.Unmarshal (bytes, &boxes)
for i := range boxes {
fmt.Printf("Id = %v, Name = %v", boxes[i].Id, boxes[i].Name)
fmt.Println()
}
I’m fairly new to Go so any help/tips/corrections will be appreciated.