I just started learning Go and I'm trying to iterate through each element of array of JSON objects.
I tried the following.
package main
import (
"encoding/json"
"fmt"
)
type itemdata []string
func main() {
var birds itemdata
birdJson := `[{"species":"pigeon","decription":"likes to perch on rocks"},{"species":"eagle","description":"bird of prey"},{"species":"eagle","description":"bird of prey"}]`
json.Unmarshal([]byte(birdJson), &birds)
fmt.Println(len(birds))
fmt.Println(birds)
for i := range birds {
fmt.Println(i)
fmt.Println(birds[i])
}
}
How can I iterate on each JSON object?
Expected Output:
0
{"species":"pigeon","decription":"likes to perch on rocks"}
1
{"species":"eagle","description":"bird of prey"}
2
{"species":"eagle","description":"bird of prey"}