我有一個有趣的 JSON 文件,它有點(diǎn)嵌套,我無法很好地舉例說明如何正確解析它。有人可以看看下面,看看我的結(jié)構(gòu)是否正確,我可以解析根級別的項目,但我嘗試越深入,我就會迷路。請在下面查看我的代碼:我嘗試解析的 JSON 文件位于單獨(dú)的文件中:{ "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" } ] },我的結(jié)構(gòu)設(shè)置在下面的 Go 文件中:package mainimport ( "encoding/json" "fmt" "io/ioutil" "os")// Report structtype 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"` } } }}
1 回答

料青山看我應(yīng)如是
TA貢獻(xiàn)1772條經(jīng)驗 獲得超8個贊
unit_report您的代碼的問題是您希望json數(shù)據(jù)與SetReport結(jié)構(gòu)匹配Go。
為此,您可以設(shè)置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
...
}
- 1 回答
- 0 關(guān)注
- 145 瀏覽
添加回答
舉報
0/150
提交
取消