gpt4 book ai didi

json - 关于通过Struct解析嵌套JSON的问题

转载 作者:行者123 更新时间:2023-12-01 22:33:28 25 4
gpt4 key购买 nike

我有一个有趣的JSON文件,它有点嵌套,我无法很好地说明如何正确解析它的好例子。有人可以在下面看看我的结构是否正确,我可以解析根级别的项目,但是如果尝试得更深入,就会迷失方向。请在下面查看我的代码:
我尝试解析的JSON文件在一个单独的文件中:

{
"pushed": 090909099,
"job_id": 17422,
"processed": 159898989,
"unit_report": [
{
"meta": {
"file": {
"file_type": "Binary",
"file_name": "Bob.txt",
"file_path": "/usr/local/Bob.txt",
"size": 4563,
"entropy": 3.877,
"hashes": [
{
"name": "Uniq34",
"value": "02904234234234234243"
},
{
"name": "sha1",
"value": "23423423423423423423423"
},
{
"name": "sha256",
"value": "523412423424234234234"
}
]
},
我的结构设置在下面的Go文件中:
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
)

// Report struct
type Report struct {
Pushed int `json:"pushed"`
JobID int `json:"job_id"`
Processed int `json:"processed"`
SetReport []struct {
Meta struct {
File struct {
FileType string `json:"file_type"`
FileName string `json:"file_name"`
FilePath string `json:"file_path"`
Size int `json:"size"`
Entropy int `json:"entropy"`
}
}
}
}

最佳答案

代码的问题是,您希望unit_report数据中的jsonSetReport结构中的Go匹配。
为此,您可以将json:"unit_report设置为SetReport字段,或者将SetReport重命名为UnitReport
要么:

Processed int `json:"processed"`
SetReport []struct {

...

} `json:"unit_report` // See the changes here
要么:
Processed int `json:"processed"`
UnitReport []struct { // See the changes here
...
}

关于json - 关于通过Struct解析嵌套JSON的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63725709/

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