大佬教程收集整理的这篇文章主要介绍了使用一些已知和一些未知的字段名称解组 JSON,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
一种选择是解组两次:一次进入一个类型的值,Foo
一次进入一个类型的值@H_152_5@map[String]interface{}并删除键"a"
和"b"
:
type Foo struct {
A int `Json:"a"`
B int `Json:"b"`
X map[String]interface{} `Json:"-"` // Rest of the fIElds should go here.
}
func main() {
s := `{"a":1, "b":2, "x":1, "y":1}`
f := Foo{}
if err := Json.Unmarshal([]byte(s), &f); err != nil {
panic(err)
}
if err := Json.Unmarshal([]byte(s), &f.X); err != nil {
panic(err)
}
delete(f.X, "a")
delete(f.X, "b")
fmt.Printf("%+v", f)
}
输出(在Go Playground上试试):
{A:1 B:2 X:map[x:1 y:1]}
另一种选择是将一次解组到 an@H_152_5@map[String]interface{}并手动处理Foo.A
和Foo.b
字段:
type Foo struct {
A int `Json:"a"`
B int `Json:"b"`
X map[String]interface{} `Json:"-"` // Rest of the fIElds should go here.
}
func main() {
s := `{"a":1, "b":2, "x":1, "y":1}`
f := Foo{}
if err := Json.Unmarshal([]byte(s), &f.X); err != nil {
panic(err)
}
if n, ok := f.X["a"].(float64); ok {
f.A = int(n)
}
if n, ok := f.X["b"].(float64); ok {
f.b = int(n)
}
delete(f.X, "a")
delete(f.X, "b")
fmt.Printf("%+v", f)
}
输出相同(Go Playground):
{A:1 B:2 X:map[x:1 y:1]}
以上是大佬教程为你收集整理的使用一些已知和一些未知的字段名称解组 JSON全部内容,希望文章能够帮你解决使用一些已知和一些未知的字段名称解组 JSON所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。