为什么这些 Golang 的 JSON Unmarshal 示例行为不同呢?

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

Why do these golang JSON Unmarshal examples behave differently?

问题

我对以下行为感到困惑,并试图弄清楚该怎么做。有人可以解释一下为什么它们的行为不同吗?为什么web_url没有被读入Bar.Url中?

package main

import (
	"encoding/json"
	"fmt"
	"testing"
)

type Foo struct {
	Web_url string
}

type Bar struct {
	Url string `json:'web_url'`
}

func TestJson(t *testing.T) {
	j := []byte(`{"web_url":"xyz"}`)

	f := &Foo{}
	err := json.Unmarshal(j, f)
	fmt.Printf("err: %v, f: %+v\n", err, f)


	b := &Bar{}
	err = json.Unmarshal(j, b)
	fmt.Printf("err: %v, b: %+v\n", err, b)
}

结果...

go test -v -run TestJson
=== RUN   TestJson
err: <nil>, f: &{Web_url:xyz}
err: <nil>, b: &{Url:}
--- PASS: TestJson (0.00s)
PASS
ok  	gitlabsr.nuq.ion.nokia.net/sr/linux/gitlab-tools/internal/junk
英文:

I'm puzzled by the following behavior and I'm trying to figure out what to do about it. Can anyone please explain to me why these behave differently? Why isn't web_url read into Bar.Url?

package main

import (
	&quot;encoding/json&quot;
	&quot;fmt&quot;
	&quot;testing&quot;
)

type Foo struct {
	Web_url string
}

type Bar struct {
	Url string `json:&#39;web_url&#39;`
}

func TestJson(t *testing.T) {
	j := []byte(`{&quot;web_url&quot;:&quot;xyz&quot;}`)

	f := &amp;Foo{}
	err := json.Unmarshal(j, f)
	fmt.Printf(&quot;err: %v, f: %+v\n&quot;, err, f)


	b := &amp;Bar{}
	err = json.Unmarshal(j, b)
	fmt.Printf(&quot;err: %v, b: %+v\n&quot;, err, b)
}

Results...

go test -v -run TestJson
=== RUN   TestJson
err: &lt;nil&gt;, f: &amp;{Web_url:xyz}
err: &lt;nil&gt;, b: &amp;{Url:}
--- PASS: TestJson (0.00s)
PASS
ok  	gitlabsr.nuq.ion.nokia.net/sr/linux/gitlab-tools/internal/junk

答案1

得分: 4

在结构标签定义中使用单引号引起了这个错误。

package main

import (
    "encoding/json"
    "fmt"
    "testing"
)

type Foo struct {
    Web_url string
}

type Bar struct {
    Url string `json:"web_url"`
}

func TestJson(t *testing.T) {
    j := []byte(`{"web_url":"xyz"}`)

    f := &Foo{}
    err := json.Unmarshal(j, f)
    fmt.Printf("err: %v, f: %+v\n", err, f)


    b := &Bar{}
    err = json.Unmarshal(j, b)
    fmt.Printf("err: %v, b: %+v\n", err, b)
}

Bar结构体中,使用单引号'代替双引号"引起了错误。应该使用双引号来定义结构标签,如json:"web_url"

英文:

Using single quotes in struct tag definition caused this error.

package main

import (
    &quot;encoding/json&quot;
    &quot;fmt&quot;
    &quot;testing&quot;
)

type Foo struct {
    Web_url string
}

type Bar struct {
    Url string `json:&quot;web_url&quot;`
}

func TestJson(t *testing.T) {
    j := []byte(`{&quot;web_url&quot;:&quot;xyz&quot;}`)

    f := &amp;Foo{}
    err := json.Unmarshal(j, f)
    fmt.Printf(&quot;err: %v, f: %+v\n&quot;, err, f)


    b := &amp;Bar{}
    err = json.Unmarshal(j, b)
    fmt.Printf(&quot;err: %v, b: %+v\n&quot;, err, b)
}

huangapple
  • 本文由 发表于 2022年8月19日 04:01:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/73408786.html
匿名

发表评论

匿名网友

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

确定