2 回答

TA貢獻(xiàn)1735條經(jīng)驗(yàn) 獲得超5個(gè)贊
您的ParseResponse函數(shù)存在問題,您正在調(diào)用json.Unmarshal作為第一個(gè)參數(shù)傳遞json,這是一個(gè)包名稱:這是模棱兩可的。
如您所見,您的代碼在更改ParseResponse函數(shù)時(shí)運(yùn)行良好。
package main
import (
"encoding/json"
"fmt"
)
type gateway struct {
Characteristics []string `json:"characteristics"`
CreatedAt string `json:"created_at"`
Credentials []interface{} `json:"credentials"`
Description interface{} `json:"description"`
GatewaySpecificFields []interface{} `json:"gateway_specific_fields"`
GatewayType string `json:"gateway_type"`
Name string `json:"name"`
PaymentMethods []string `json:"payment_methods"`
Redacted bool `json:"redacted"`
State string `json:"state"`
Token string `json:"token"`
UpdatedAt string `json:"updated_at"`
}
type gateways struct {
Gateways []gateway `json:"gateways"`
}
func ParseResponse(js []byte) {
var parsed gateways
json.Unmarshal(js, &parsed)
fmt.Println(parsed)
}
func main() {
var js []byte = []byte(`{
"gateways": [
{
"token": "my_token_here",
"gateway_type": "test",
"description": null,
"payment_methods": [
"credit_card",
"sprel",
"third_party_token",
"bank_account",
"apple_pay"
],
"state": "retained",
"created_at": "2016-03-12T18:52:37Z",
"updated_at": "2016-03-12T18:52:37Z",
"name": "Spreedly Test",
"characteristics": [
"purchase",
"authorize",
"capture",
"credit",
"general_credit",
"void",
"verify",
"reference_purchase",
"purchase_via_preauthorization",
"offsite_purchase",
"offsite_authorize",
"3dsecure_purchase",
"3dsecure_authorize",
"store",
"remove",
"disburse",
"reference_authorization"
],
"credentials": [],
"gateway_specific_fields": [],
"redacted": false
}
]
}`)
/*
var parsed gateways
e := json.Unmarshal(js, &parsed)
if e != nil {
fmt.Println(e.Error())
} else {
fmt.Println(parsed)
}
*/
ParseResponse(js)
}
輸出:
{[{[purchase authorize capture credit general_credit void verify reference_purchase purchase_via_preauthorization offsite_purchase offsite_authorize 3dsecure_purchase 3dsecure_authorize store remove disburse reference_authorization] 2016-03-12T18:52:37Z [] <nil> [] test Spreedly Test [credit_card sprel third_party_token bank_account apple_pay] false retained my_token_here 2016-03-12T18:52:37Z}]}

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超9個(gè)贊
看看http://play.golang.org/p/3xJHBmhuei - 解組到你的第二個(gè)網(wǎng)關(guān)定義成功完成,所以它一定是你缺少的東西。
檢查 json.Unmarshal 調(diào)用是否返回錯(cuò)誤。
PS 如果您成功地解組到網(wǎng)關(guān)的第一個(gè)版本,這可能不是問題,但是您上面給出的 JSON 字符串缺少右括號(hào)“}”。
- 2 回答
- 0 關(guān)注
- 228 瀏覽
添加回答
舉報(bào)