go json.Unmarshal 不起作用

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

go json.Unmarshal do not working

问题

我有一个未解码为结构体的 JSON 数据。

我知道错误出现在我的代码中的某个地方,但我卡住了,不知道错误出在哪里,以及我做错了什么。

请帮帮我,这是我的代码:

type GOOGLE_JSON struct {
    code        string `json:"code"`
    clientId    string `json:"clientId"`
    redirectUri string `json:"redirectUri"`
}

body := []byte(`{"code":"111","clientId":"222","redirectUri":"333"}`)

var google_json GOOGLE_JSON

err := json.Unmarshal(body, &google_json)
if err != nil {
    fmt.Println(err)
}
fmt.Println(google_json)

这里有一个示例

英文:

I have json which is not decoded to struct.

I know that error somewhere in my code, but I'm stuck and do not know where the error is and what am I doing wrong

Help me please, here is my code:

type GOOGLE_JSON struct {
	code        string `json:"code"`
	clientId    string `json:"clientId"`
	redirectUri string `json:"redirectUri"`
}

body := []byte(`{"code":"111","clientId":"222","redirectUri":"333"}`)

//google_json := GOOGLE_JSON{}
var google_json GOOGLE_JSON

err := json.Unmarshal(body, &google_json)
if err != nil {
	fmt.Println(err)
}
fmt.Println(google_json)

example here

答案1

得分: 10

我发现错误

type GOOGLE_JSON struct {
	code        string `json:"code"`
	clientId    string `json:"clientId"`
	redirectUri string `json:"redirectUri"`
}

必须使用大写字母

type GOOGLE_JSON struct {
	Code        string `json:"code"`
	ClientId    string `json:"clientId"`
	RedirectUri string `json:"redirectUri"`
}

我注意力不集中

Code // <- 导出的
ClientId // <- 导出的
RedirectUri // <- 导出的

code // <- 未导出的
clientId // <- 未导出的
redirectUri // <- 未导出的
英文:

I've found error

was

 	type GOOGLE_JSON struct {
		code        string `json:&quot;code&quot;`
		clientId    string `json:&quot;clientId&quot;`
		redirectUri string `json:&quot;redirectUri&quot;`
	}

must be capital letters

 	type GOOGLE_JSON struct {
		Code        string `json:&quot;code&quot;`
		ClientId    string `json:&quot;clientId&quot;`
		RedirectUri string `json:&quot;redirectUri&quot;`
	}

I was inattentive

Code // &lt;- exported
ClientId // &lt;- exported
RedirectUri // &lt;- exported

code // &lt;-- not exported
clientId // &lt;-- not exported
redirectUri // &lt;-- not exported

huangapple
  • 本文由 发表于 2015年3月22日 19:02:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/29193556.html
匿名

发表评论

匿名网友

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

确定