[英]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
在結構標記定義中使用單引號會導致此錯誤。
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)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.