gpt4 book ai didi

json - 用大数组解析 json

转载 作者:行者123 更新时间:2023-12-01 22:33:27 24 4
gpt4 key购买 nike

官方指南只是展示了一个简单的例子来解析一个大数组的 json:

[ // eat this
{"Name": "Ed", "Text": "Knock knock."}, /*
{"Name": "Sam", "Text": "Who's there?"}, *
{"Name": "Ed", "Text": "Go fmt."}, * parse this part one by one
{"Name": "Sam", "Text": "Go fmt who?"}, *
{"Name": "Ed", "Text": "Go fmt yourself!"} */
] // eat this
但是这种方式不能在我的json中使用:
{
"A":[
{"a":"xxx","b":"xxx"} /* A is useless. */
],
"B":[
{"c":"xxx","d":"xxx"}, /*
{"c":"xxx","d":"xxx"}, * B has thousands of children.
{"c":"xxx","d":"xxx"} */
...
]
}
如何逐个解析B中的 child (而不是将它们全部读入内存)

最佳答案

如果我正确理解你的问题,也许这个例子会有所帮助
包主

import (
"fmt"
"encoding/json"
)
type Example struct {
B []struct {
C string `json:"c"`
D string `json:"d"`
} `json:"B"`
}
func main() {
var str = `{"A":[{"a":"xxx","b":"xxx"}],"B":[{"c":"xxx","d":"xxx"},{"c":"xxx","d":"xxx"}]}`
var elem Example
json.Unmarshal([]byte(str),&elem)
fmt.Printf("elem:%+v",elem)

}

关于json - 用大数组解析 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63815172/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com