json.Unmarshal returning blank structure

亡梦爱人 提交于 2019-11-27 09:29:35

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
}

See it working on the Go Playground here

You should also reference the Golang specification for answers. Specifically, the part that talks about Exported Identifiers.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!