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

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

Parsing JSON in Golang doesn't Populate Object

问题

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

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

  1. import (
  2. "encoding/json"
  3. "fmt"
  4. "strings"
  5. )
  6. type RefreshTokenData struct {
  7. id string `json:"id"`
  8. issued_at string `json:"issued_at"`
  9. scope string `json:"scope"`
  10. instance_url string `json:"instance_url"`
  11. token_type string `json:"token_type"`
  12. refresh_token string `json:"refresh_token"`
  13. signature string `json:"signature"`
  14. access_token string `json:"access_token"`
  15. }
  16. func main() {
  17. var tokenResp = `
  18. {"id":"https://google.com","issued_at":"1423698767063",
  19. "scope":"full refresh_token",
  20. "instance_url":"https://na15.salesforce.com",
  21. "token_type":"Bearer",
  22. "refresh_token":"2os53__CCU5JX_yZXE",
  23. "id_token":"5jSH0Oqm7Q4fc0xkE9NOvW8cA13U",
  24. "signature":"/599EkGVIBsKPFRNkg+58wZ3Q7AFyclvIGvCrxVeyTo=",
  25. "access_token":"sadfasdfasdfasdfdsa"}`
  26. var tokenData RefreshTokenData
  27. decoder := json.NewDecoder(strings.NewReader(tokenResp))
  28. if jsonerr := decoder.Decode(&tokenData); jsonerr != nil {
  29. fmt.Println("****Failed to decode json")
  30. } else {
  31. fmt.Println("****Refresh token: " + tokenData.refresh_token)
  32. }
  33. }

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

英文:

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

  1. import (
  2. "encoding/json"
  3. "fmt"
  4. "strings"
  5. )
  6. type RefreshTokenData struct {
  7. id string `json:"id"`
  8. issued_at string `json:"issued_at"`
  9. scope string `json:"scope"`
  10. instance_url string `json:"instance_url"`
  11. token_type string `json:"token_type"`
  12. refresh_token string `json:"refresh_token"`
  13. signature string `json:"signature"`
  14. access_token string `json:"access_token"`
  15. }
  16. func main() {
  17. var tokenResp = `
  18. {"id":"https://google.com","issued_at":"1423698767063",
  19. "scope":"full refresh_token",
  20. "instance_url":"https://na15.salesforce.com",
  21. "token_type":"Bearer",
  22. "refresh_token":"2os53__CCU5JX_yZXE",
  23. "id_token":"5jSH0Oqm7Q4fc0xkE9NOvW8cA13U",
  24. "signature":"/599EkGVIBsKPFRNkg+58wZ3Q7AFyclvIGvCrxVeyTo=",
  25. "access_token":"sadfasdfasdfasdfdsa"}`
  26. var tokenData RefreshTokenData
  27. decoder := json.NewDecoder(strings.NewReader(tokenResp))
  28. if jsonerr := decoder.Decode(&tokenData); jsonerr != nil {
  29. fmt.Println("****Failed to decode json")
  30. } else {
  31. fmt.Println("****Refresh token: " + tokenData.refresh_token)
  32. }
  33. }

答案1

得分: 3

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

  1. type RefreshTokenData struct {
  2. Id string `json:"id"`
  3. Issued_at string `json:"issued_at"`
  4. Scope string `json:"scope"`
  5. Instance_url string `json:"instance_url"`
  6. Token_type string `json:"token_type"`
  7. Refresh_token string `json:"refresh_token"`
  8. Signature string `json:"signature"`
  9. Access_token string `json:"access_token"`
  10. }

playground示例

英文:

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

  1. type RefreshTokenData struct {
  2. Id string `json:"id"`
  3. Issued_at string `json:"issued_at"`
  4. Scope string `json:"scope"`
  5. Instance_url string `json:"instance_url"`
  6. Token_type string `json:"token_type"`
  7. Refresh_token string `json:"refresh_token"`
  8. Signature string `json:"signature"`
  9. Access_token string `json:"access_token"`
  10. }

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:

确定