在Golang中解析JSON时,无法填充对象。

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

Parsing JSON in Golang doesn't Populate Object

问题

作为Oauth应用程序的一部分,我需要解码一些JSON数据。但是我无法获取到填充的对象。没有出现错误,但数据就是不在那里。我尝试了很多不同的方法...

我已经在http://play.golang.org/p/QGkcl61cmv上重新创建了这个问题。

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

type RefreshTokenData struct {
	id            string `json:"id"`
	issued_at     string `json:"issued_at"`
	scope         string `json:"scope"`
	instance_url  string `json:"instance_url"`
	token_type    string `json:"token_type"`
	refresh_token string `json:"refresh_token"`
	signature     string `json:"signature"`
	access_token  string `json:"access_token"`
}

func main() {
	var tokenResp = `
	{"id":"https://google.com","issued_at":"1423698767063",
	"scope":"full refresh_token",
	"instance_url":"https://na15.salesforce.com",
	"token_type":"Bearer",
	"refresh_token":"2os53__CCU5JX_yZXE",
	"id_token":"5jSH0Oqm7Q4fc0xkE9NOvW8cA13U",
	"signature":"/599EkGVIBsKPFRNkg+58wZ3Q7AFyclvIGvCrxVeyTo=",
	"access_token":"sadfasdfasdfasdfdsa"}`

	var tokenData RefreshTokenData
	decoder := json.NewDecoder(strings.NewReader(tokenResp))
	if jsonerr := decoder.Decode(&tokenData); jsonerr != nil {
		fmt.Println("****Failed to decode json")
	} else {
		fmt.Println("****Refresh token: " + tokenData.refresh_token)
	}
}

希望这可以帮助你解决问题。

英文:

As part of an Oauth application, I need to decode some JSON. But I cannot get the object populated. There is no failure, but the data just isn't there. I've tried a bunch of different ways...

I have recreated the problem at http://play.golang.org/p/QGkcl61cmv

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

type RefreshTokenData struct {
	id            string `json:"id"`
	issued_at     string `json:"issued_at"`
	scope         string `json:"scope"`
	instance_url  string `json:"instance_url"`
	token_type    string `json:"token_type"`
	refresh_token string `json:"refresh_token"`
	signature     string `json:"signature"`
	access_token  string `json:"access_token"`
}

func main() {
	var tokenResp = `
	{"id":"https://google.com","issued_at":"1423698767063",
	"scope":"full refresh_token",
	"instance_url":"https://na15.salesforce.com",
	"token_type":"Bearer",
	"refresh_token":"2os53__CCU5JX_yZXE",
	"id_token":"5jSH0Oqm7Q4fc0xkE9NOvW8cA13U",
	"signature":"/599EkGVIBsKPFRNkg+58wZ3Q7AFyclvIGvCrxVeyTo=",
	"access_token":"sadfasdfasdfasdfdsa"}`
	
	var tokenData RefreshTokenData
	decoder := json.NewDecoder(strings.NewReader(tokenResp))
	if jsonerr := decoder.Decode(&tokenData); jsonerr != nil {
		fmt.Println("****Failed to decode json")
	} else {
		fmt.Println("****Refresh token: " + tokenData.refresh_token)
	}
}

答案1

得分: 3

JSON编码包仅适用于导出字段。将字段名大写以导出它们:

type RefreshTokenData struct {
    Id            string `json:"id"`
    Issued_at     string `json:"issued_at"`
    Scope         string `json:"scope"`
    Instance_url  string `json:"instance_url"`
    Token_type    string `json:"token_type"`
    Refresh_token string `json:"refresh_token"`
    Signature     string `json:"signature"`
    Access_token  string `json:"access_token"`
}

playground示例

英文:

The JSON encoding package works with exported fields only. Capitalize the field names to export them:

type RefreshTokenData struct {
  Id            string `json:"id"`
  Issued_at     string `json:"issued_at"`
  Scope         string `json:"scope"`
  Instance_url  string `json:"instance_url"`
  Token_type    string `json:"token_type"`
  Refresh_token string `json:"refresh_token"`
  Signature     string `json:"signature"`
  Access_token  string `json:"access_token"`
}

playground example

huangapple
  • 本文由 发表于 2015年2月12日 08:38:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/28467302.html
匿名

发表评论

匿名网友

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

确定