【发布时间】:2018-07-22 20:47:04
【问题描述】:
我刚开始学习 Go,我正在尝试遍历 JSON 对象数组的每个元素。
我尝试了以下方法。
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])
}
}
如何迭代每个 JSON 对象?
预期输出:
0
{"species":"pigeon","decription":"likes to perch on rocks"}
1
{"species":"eagle","description":"bird of prey"}
2
{"species":"eagle","description":"bird of prey"}
【问题讨论】:
标签: arrays json loops go iteration