簡體   English   中英

用大數組解析json

[英]parse 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

但是這種方式不能在我的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中的孩子(不是全部讀入內存)

如果我正確理解你的問題,也許這個例子可能會有所幫助 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