英文:
json.Unmarshal returning blank structure
问题
我有一个看起来像这样的 JSON 数据块:
{
"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:[]}
我尝试先分配结构体,但也没有起作用,我不确定为什么它没有生成值,并且没有返回错误。
英文:
I have a JSON blob that looks like this
{
"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"
}]
}
and I have a little code to transform that back into go structs
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)
but what I see when I run this is just a zeroed structure
main.Mydata: {met:{to: from: id: entryCount:0 size:0 depricated:false} stat:[]}
I tried allocating the struct first but that also didn't work, I'm not sure why its not producing values, and its not returning an error
答案1
得分: 99
你的结构字段没有被导出。这是因为它们以小写字母开头。
EntryCount // <--- 被导出
entryCount // <--- 未被导出
当我说"未被导出"时,意思是它们在包外不可见。你的包可以自由访问它们,因为它们在包内部有作用域。
然而,对于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
}
<kbd>在Go Playground上查看它的工作示例</kbd>
你还应该参考Golang规范以获取答案。具体来说,参考讨论导出标识符的部分。
英文:
Your struct fields are not exported. This is because they start with a lowercase letter.
EntryCount // <--- Exported
entryCount // <--- Not exported
When I say "not exported", I mean they are not visible outside of your package. Your package can happily access them because they are scoped locally to it.
As for the encoding/json
package though - it cannot see them. You need to make all of your fields visible to the encoding/json
package by making them all start with an uppercase letter, thereby exporting them:
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
}
<kbd>See it working on the Go Playground here</kbd>
You should also reference the Golang specification for answers. Specifically, the part that talks about Exported Identifiers.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论