
无法使用 go lang 将嵌套 json 解析为结构对象
我有一个嵌套的 json 字符串,我想使用 go 语言中的结构体来解析它。 json 看起来像这样
{"action":"add","business":{"listid":123,"objecttags":[{"tagcode":"csharp","tagname":"codename","tagvalue":["2"],"tagtype":3},{"tagcode":"golang","tagname":"coding","tagvalue":["3"],"tagtype":3}]}}我想用go语言解析json。 json 具有嵌套结构,因此我创建了以下代码中提到的结构
package main
import (
"encoding/json"
"fmt"
)
type objecttagslist struct {
tagcode string
tagname string
tagvalue []string
}
type model struct {
action string `json:"action"`
business struct {
listid int64 `json:"listid"`
objecttags []objecttagslist `json:"objecttags"`
} `json:"business"`
}
func main() {
json := `{"action":"add","business":{"listid":123,"objecttags":[{"tagcode":"csharp","tagname":"codename","tagvalue":["2"],"tagtype":3},{"tagcode":"golang","tagname":"coding","tagvalue":["3"],"tagtype":3}]}}`
var model model
json.unmarshal([]byte(json), &model)
fmt.println(model.action) // this prints correctly as "add"
fmt.println(model.business.listid) // this prints correctly as "123"
fmt.println(model.business.objecttags) // this does not print the objecttags. rather this prints the objecttags as "[{ []} { []}]"
}我无法将内部嵌套 json 的值获取到结构中。
我还尝试再次解组内部结构
立即学习“go语言免费学习笔记(深入)”;
var object []objecttagslist //this gives error as cannot convert model.business.objecttags (variable of type []objecttagslist) to type []byte json.unmarshal([]byte(model.business.objecttags), &object)
//错误,无法将 model.business.objecttags([]objecttagslist 类型的变量)转换为 []byte 类型
Easily find JSON paths within JSON objects using our intuitive Json Path Finder
30
fmt.println(object)
这给了我一个错误 无法将 model.business.objecttags([]objecttagslist 类型的变量)转换为 []byte 类型。
如何将此 json 映射到结构中? 我想以这样的方式映射它,以便我可以使用像
这样的对象model.Business.ObjectTags[0].tagCode //--> Should print/store "csharp" model.Business.ObjectTags[0].tagValue[0] //--> Should print/store "2"
请帮忙
您只能编组/取消编组“导出”字段——即可以在当前包外部访问的字段,这在 go 中意味着“以大写字母开头的字段”。因此,如果您要将代码修改为如下所示:
package main
import (
"encoding/json"
"fmt"
)
type objecttagslist struct {
tagcode string
tagname string
tagvalue []string
}
type model struct {
action string `json:"action"`
business struct {
listid int64 `json:"listid"`
objecttags []objecttagslist `json:"objecttags"`
} `json:"business"`
}
func main() {
json := `
{
"action": "add",
"business": {
"listid": 123,
"objecttags": [
{
"tagcode": "csharp",
"tagname": "codename",
"tagvalue": [
"2"
],
"tagtype": 3
},
{
"tagcode": "golang",
"tagname": "coding",
"tagvalue": [
"3"
],
"tagtype": 3
}
]
}
}
`
var model model
json.unmarshal([]byte(json), &model)
fmt.println(model.action)
fmt.println(model.business.listid)
fmt.println(model.business.objecttags)
}您将得到输出:
add
123
[{csharp codename [2]} {golang coding [3]}]这里我们利用了 json 模块会自动将名为 tagcode 的键映射到名为 tagcode 的结构体字段的事实,但实际上我们应该明确:
type ObjectTagsList struct {
TagCode string `json:"tagCode"`
TagName string `json:"tagName"`
TagValue []string `json:"tagValue"`
}以上就是go语言中使用struct解析嵌套的json的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号