简体   繁体   English

用大数组解析json

[英]parse json with a big array

The official guide just shows a simple example to parse a json with a big array:官方指南只是展示了一个简单的例子来解析一个带有大数组的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

but this way can not be used in my json:但是这种方式不能在我的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"}     */
    ...
]
}

How to parse children in B one by one(not read them all into memory)如何一一解析B中的孩子(不是全部读入内存)

If I understand your question correctly, perhaps this example can be helpful package main如果我正确理解你的问题,也许这个例子可能会有所帮助 package main

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)
    
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM