JSON Unmarshall与结构体一起使用时无法按预期工作

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

JSON Unmarshall not working as expected with structs

问题

以下是代码的翻译:

package main

import "encoding/json"
import "fmt"

type SuperNum struct {
    num string
}

func main() {
    byt := []byte(`{"num":"6.13"}`)

    var dat SuperNum

    if err := json.Unmarshal(byt, &dat); err != nil {
        panic(err)
    }
    fmt.Printf("%+v", dat) // 我期望这里有一个 `num` 属性
}

输出结果:

{num:}
程序已退出。

你可以在 [golang playground](http://play.golang.org/p/lU9qNrdB-Y) 上运行这段代码。

因为我在结构体和 JSON 中都设置了一个 `num` 属性,并且它们都是字符串,所以我期望 `dat` 结构体应该有一个 `num` 属性,值为 `hello`,但实际上没有。

我做错了什么?我对这个工作原理的理解有什么错误?

**编辑**

我尝试给结构体添加了 `json` 标签,但没有任何区别(不知道它实际上是做什么的)。

```go
type SuperNum struct {
    num string `json:"num"`
}
英文:

I have the following code:

package main

import "encoding/json"
import "fmt"

type SuperNum struct {
	num string
}

func main() {
    byt := []byte(`{"num":"6.13"}`)
   
    var dat SuperNum

    if err := json.Unmarshal(byt, &dat); err != nil {
        panic(err)
    }
    fmt.Printf("%+v", dat) // I expect this to have a `num` attribute
}

Output:

{num:}
Program exited.

You can run this code in the golang playground.

Because I'm setting a num property in the struct and in the JSON and they're both strings, I would have expected the dat struct to have a num property, with 'hello', but it doesn't.

What am I doing wrong? What in my mental model of how this should work is incorrect?

EDIT

I tried adding the json signature to the struct, but it makes no difference (no idea what that actually does).

type SuperNum struct {
	num string `json:"num"`
}

答案1

得分: 4

num按照惯例不会被导出,因为它是小写的。将其改为Num,你就可以检查结果了。

type SuperNum struct {
    Num string
}
英文:

num is by convention not exported as it is lower case. Change it to Num and you are able to inspect the result.

type SuperNum struct {
	Num string
}                                                                                   

答案2

得分: 2

只需将num更改为Num。结构体的小写属性不可见。

<kbd>Go playground</kbd>

英文:

Just change num to Num. The lowercase properties of the structures are not visible.

<kbd>Go playground</kbd>

答案3

得分: 1

当解组 JSON 结构时,你映射的属性必须是公开的(请记住,在 Go 中,结构和模块成员的公开和私有可见性由成员的名称是大写还是小写驼峰命名来表示)。

所以,首先,你的结构必须像这样定义:

type SuperNum struct {
    Num string  // <- 注意大写的 "N"
}

有了这个结构,JSON 解组器将期望 JSON 属性也被命名为 Num。如果要配置不同的属性名称(例如你的示例中的小写 num),请使用 json 注释来配置该结构成员:

type SuperNum struct {
    Num string `json:"num"`
}
英文:

When unmarhalling JSON structures, the properties that you're mapping on must be public (remember that in Go, public and private visibility of struct and module members is denoted by the member's name being upper or lower camel case.

So, first of all, your struct must be defined like this:

type SuperNum struct {
    Num string  // &lt;- note the capital &quot;N&quot;
}

With this struct, the JSON marshaller will expect the JSON property to be also named Num. In order to configure a different property name (like the lowercased num in your example), use the json annotation for that struct member:

type SuperNum struct {
    Num string `json:&quot;num&quot;`
}

huangapple
  • 本文由 发表于 2015年12月31日 05:34:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/34537588.html
匿名

发表评论

匿名网友

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

确定