[英]json.Unmarshal returning blank structure
我有一個看起來像這樣的JSON Blob
{
"metadata":{
"id":"2377f625-619b-4e20-90af-9a6cbfb80040",
"from":"2014-12-30T07:23:42.000Z",
"to":"2015-01-14T05:11:51.000Z",
"entryCount":801,
"size":821472,
"deprecated":false
},
"status":[{
"node_id":"de713614-be3d-4c39-a3f8-1154957e46a6",
"status":"PUBLISHED"
}]
}
我有一些代碼將其轉換回go結構
type Status struct {
status string
node_id string
}
type Meta struct {
to string
from string
id string
entryCount int64
size int64
depricated bool
}
type Mydata struct {
met meta
stat []status
}
var realdata Mydata
err1 := json.Unmarshal(data, &realdata)
if err1 != nil {
fmt.Println("error:", err1)
}
fmt.Printf("%T: %+v\n", realdata, realdata)
但是我在運行時看到的只是一個零位結構
main.Mydata: {met:{to: from: id: entryCount:0 size:0 depricated:false} stat:[]}
我嘗試先分配該結構,但這也沒有用,我不確定為什么它不產生值,並且不返回錯誤
您的結構字段不會導出。 這是因為它們以小寫字母開頭。
EntryCount // <--- Exported
entryCount // <--- Not exported
當我說“未導出”時,是指它們在您的包裝之外不可見。 您的軟件包可以愉快地訪問它們,因為它們在本地作用域內。
至於encoding/json
包-它看不到它們。 您需要通過將所有字段都以大寫字母開頭,從而導出它們,從而使所有字段對encoding/json
包可見:
type Status struct {
Status string
Node_id string
}
type Meta struct {
To string
From string
Id string
EntryCount int64
Size int64
Depricated bool
}
type Mydata struct {
Metadata Meta
Status []Status
}
您還應該參考Golang規范以獲得答案。 具體來說, 是有關導出標識符的部分 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.