json.Unmarshal 返回空结构体

huangapple go评论111阅读模式
英文:

json.Unmarshal returning blank structure

问题

我有一个看起来像这样的 JSON 数据块:

  1. {
  2. "metadata":{
  3. "id":"2377f625-619b-4e20-90af-9a6cbfb80040",
  4. "from":"2014-12-30T07:23:42.000Z",
  5. "to":"2015-01-14T05:11:51.000Z",
  6. "entryCount":801,
  7. "size":821472,
  8. "deprecated":false
  9. },
  10. "status":[{
  11. "node_id":"de713614-be3d-4c39-a3f8-1154957e46a6",
  12. "status":"PUBLISHED"
  13. }]
  14. }

我有一段代码将其转换回 Go 结构体:

  1. type Status struct {
  2. status string
  3. node_id string
  4. }
  5. type Meta struct {
  6. to string
  7. from string
  8. id string
  9. entryCount int64
  10. size int64
  11. depricated bool
  12. }
  13. type Mydata struct {
  14. met Meta
  15. stat []Status
  16. }
  17. var realdata Mydata
  18. err1 := json.Unmarshal(data, &realdata)
  19. if err1 != nil {
  20. fmt.Println("error:", err1)
  21. }
  22. fmt.Printf("%T: %+v\n", realdata, realdata)

但是当我运行这段代码时,我只看到一个零值结构:

  1. main.Mydata: {met:{to: from: id: entryCount:0 size:0 depricated:false} stat:[]}

我尝试先分配结构体,但也没有起作用,我不确定为什么它没有生成值,并且没有返回错误。

英文:

I have a JSON blob that looks like this

  1. {
  2. "metadata":{
  3. "id":"2377f625-619b-4e20-90af-9a6cbfb80040",
  4. "from":"2014-12-30T07:23:42.000Z",
  5. "to":"2015-01-14T05:11:51.000Z",
  6. "entryCount":801,
  7. "size":821472,
  8. "deprecated":false
  9. },
  10. "status":[{
  11. "node_id":"de713614-be3d-4c39-a3f8-1154957e46a6",
  12. "status":"PUBLISHED"
  13. }]
  14. }

and I have a little code to transform that back into go structs

  1. type Status struct {
  2. status string
  3. node_id string
  4. }
  5. type Meta struct {
  6. to string
  7. from string
  8. id string
  9. entryCount int64
  10. size int64
  11. depricated bool
  12. }
  13. type Mydata struct {
  14. met meta
  15. stat []status
  16. }
  17. var realdata Mydata
  18. err1 := json.Unmarshal(data, &realdata)
  19. if err1 != nil {
  20. fmt.Println("error:", err1)
  21. }
  22. fmt.Printf("%T: %+v\n", realdata, realdata)

but what I see when I run this is just a zeroed structure

  1. 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.

  1. EntryCount // &lt;--- Exported
  2. entryCount // &lt;--- 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:

  1. type Status struct {
  2. Status string
  3. Node_id string
  4. }
  5. type Meta struct {
  6. To string
  7. From string
  8. Id string
  9. EntryCount int64
  10. Size int64
  11. Depricated bool
  12. }
  13. type Mydata struct {
  14. Metadata Meta
  15. Status []Status
  16. }

<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.

huangapple
  • 本文由 发表于 2015年1月30日 10:42:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/28228393.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定