gpt4 book ai didi

json - 将复杂的 JSON 结构初始化为 Golang 中的映射

转载 作者:IT王子 更新时间:2023-10-29 02:35:05 27 4
gpt4 key购买 nike

我需要为函数提供一个 map[string]interface{}。后面的 JSON 是这个:


{
"update": {
"comment": [
{
"add": {
"body": "this is a body"
}
}
]
}
}

我完全卡住了。我尝试使用嵌套结构、映射以及两者的混合,我只是看不到这个简单事物的解决方案..

我最后一次尝试是:

    // Prepare the data
var data = make(map[string]interface{})
var comments []map[string]map[string]string
var comment = make(map[string]map[string]string)
comment["add"] = map[string]string{
"body": "Test",
}
comments = append(comments, comment)
data["update"]["comment"] = comments

最佳答案

通常人们为此使用 interface{}Unmarshal()!

查看一些例子

希望对您有所帮助! :)

关于json - 将复杂的 JSON 结构初始化为 Golang 中的映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56871627/

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