json.Unmarshal 返回空结构体

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

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 // &lt;--- Exported
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:

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.

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:

确定