GO语言解码JSON(简单数组未被捕获)

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

GO Lang decode JSON (simple array not being picked up)

问题

结构体和JSON在Go语言中并不是很有趣。

我有一个简单的JSON示例和一个结构体。一切似乎都被正确解析了,但是由于某种原因数组没有被获取到。

有人能告诉我可能遗漏了什么吗?

代码---
package main

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

type Oauth struct {
Aud string   `json:"aud"`
Cid string   `json:"cid"`
Exp int      `json:"exp"`
Iat int      `json:"iat"`
Iss string   `json:"iss"`
Jti string   `json:"jti"`
Scp []string `json:"scp"`
Sub string   `json:"sub"`
UID string   `json:"uid"`
Ver int      `json:"ver"`
}

func main () {

// 这是字符串 { "ver":1,"jti":"AT.zgv9oQpw-7l3BCg6Xb5NCG2Pf8zxgiQa1EUBXycmaDk","iss":"https://companyx.okta.com/oauth2/aus1a4ibdat0JYw5s1d8","aud":"http://localhost","iat":1484538606,"exp":1484542206,"cid":"3jmNvVCFZ5F6lWOzIONO","uid":"00uy74c0h7NGTLBSXQOC","scp":["read","remove","reserve"],"sub":"oktaadmin@okta.com"}
encoded := "eyJ2ZXIiOjEsImp0aSI6IkFULnpndjlvUXB3LTdsM0JDZzZYYjVOQ0cyUGY4enhnaVFhMUVVQlh5Y21hRGsiLCJpc3MiOiJodHRwczovL2NvbXBhbnl4Lm9rdGEuY29tL29hdXRoMi9hdXMxYTRpYmRhdDBKWXc1czFkOCIsImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3QiLCJpYXQiOjE0ODQ1Mzg2MDYsImV4cCI6MTQ4NDU0MjIwNiwiY2lkIjoiM2ptTnZWQ0ZaNUY2bFdPeklPTk8iLCJ1aWQiOiIwMHV5NzRjMGg3TkdUTEJTWFFPQyIsInNjcCI6WyJyZWFkIiwicmVtb3ZlIiwicmVzZXJ2ZSJdLCJzdWIiOiJva3RhYWRtaW5Ab2t0YS5jb20ifQ"

data, _ := base64.StdEncoding.DecodeString(encoded)

fmt.Println(string(data))
fmt.Println ("")

var x Oauth
json.Unmarshal([]byte(data), &x)
fmt.Printf ("%+v",x.Scp);

}

结果始终是一个空数组 []

英文:

Structs and JSON are not super fun in Go.

I have a simple example of some JSON, and a struct. Everything seems to get parsed Okay, but for some reason the array does not get picked up.

Can anyone tell me what I might be missing.

Code---
package main

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

type Oauth struct {
Aud string   `json:"aud"`
Cid string   `json:"cid"`
Exp int      `json:"exp"`
Iat int      `json:"iat"`
Iss string   `json:"iss"`
Jti string   `json:"jti"`
Scp []string `json:"scp"`
Sub string   `json:"sub"`
UID string   `json:"uid"`
Ver int      `json:"ver"`
}

func main () {

// This is the String {"ver":1,"jti":"AT.zgv9oQpw-7l3BCg6Xb5NCG2Pf8zxgiQa1EUBXycmaDk","iss":"https://companyx.okta.com/oauth2/aus1a4ibdat0JYw5s1d8","aud":"http://localhost","iat":1484538606,"exp":1484542206,"cid":"3jmNvVCFZ5F6lWOzIONO","uid":"00uy74c0h7NGTLBSXQOC","scp":["read","remove","reserve"],"sub":"oktaadmin@okta.com"}
encoded := "eyJ2ZXIiOjEsImp0aSI6IkFULnpndjlvUXB3LTdsM0JDZzZYYjVOQ0cyUGY4enhnaVFhMUVVQlh5Y21hRGsiLCJpc3MiOiJodHRwczovL2NvbXBhbnl4Lm9rdGEuY29tL29hdXRoMi9hdXMxYTRpYmRhdDBKWXc1czFkOCIsImF1ZCI6Imh0dHA6Ly9sb2NhbGhvc3QiLCJpYXQiOjE0ODQ1Mzg2MDYsImV4cCI6MTQ4NDU0MjIwNiwiY2lkIjoiM2ptTnZWQ0ZaNUY2bFdPeklPTk8iLCJ1aWQiOiIwMHV5NzRjMGg3TkdUTEJTWFFPQyIsInNjcCI6WyJyZWFkIiwicmVtb3ZlIiwicmVzZXJ2ZSJdLCJzdWIiOiJva3RhYWRtaW5Ab2t0YS5jb20ifQ"

data, _ := base64.StdEncoding.DecodeString(encoded)

fmt.Println(string(data))
fmt.Println ("")

var x Oauth
json.Unmarshal([]byte(data), &x)
fmt.Printf ("%+v",x.Scp);

}

The result is always an Empty Array []

答案1

得分: 2

编码后的字符串不是有效的 JSON,很容易检测到,因为您在 Unmarshal 上忽略了一个重要的错误,请尝试以下代码:

err := json.Unmarshal(data, &x)
fmt.Println(err)

看起来您错过了 JSON 的最后一个 '}'。

英文:

The encoded string isn't a valid JSON, easy to detect because you're ignoring an important error on Unmarshal, try this:

err := json.Unmarshal(data, &x)
fmt.Println(err)

It looks like you missed the last '}' of your JSON.

答案2

得分: 0

你的 base64 编码字符串无效:

输入字节 404 处存在非法的 base64 数据。

英文:

Your base64-encoded string is not valid:

illegal base64 data at input byte 404

huangapple
  • 本文由 发表于 2017年1月16日 12:27:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/41669446.html
匿名

发表评论

匿名网友

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

确定