gpt4 book ai didi

json - 将具有重复字段的字符串解码为 json

转载 作者:IT王子 更新时间:2023-10-29 02:33:54 31 4
gpt4 key购买 nike

尝试将字符串解码为 json,但我的结构定义不起作用。如何修复?

package main

import "fmt"
import "encoding/json"

func main() {
x := `{
"Header": {
"Encoding-Type": [
"gzip"
],
"Bytes": [
"29"
]
}
}`

type HeaderStruct struct {
A string
B []string
}
type Foo struct {
Header HeaderStruct
}


var f Foo
if e := json.Unmarshal([]byte(x), &f); e != nil {
fmt.Println("Failed:", e)
} else {
fmt.Println("unmarshalled=", f)
}

}

最佳答案

您的变量名称与 json 键的名称不匹配,并且它们都是 []string。你可以做

type HeaderStruct struct {
A []string `json:"Encoding-Type"`
B []string `json:"Bytes"
}

关于json - 将具有重复字段的字符串解码为 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32260877/

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