将GoLang中的JSON解析为struct [英] Parsing JSON in GoLang into struct
问题描述
因此,我在使用golang解析这些数据时遇到了一些麻烦:
{
网关: [
{
token:my_token_here,
gateway_type:test,
description:null,
payment_methods:[
credit_card,
sprel,
third_party_token,
bank_account,
apple_pay
,
状态:保留,
created_at:2016-03-12T18:52:37Z,
updated_at:2016-03-12T18:52:37Z,
name:Spreedly Test,
特征:[
购买,
授权,
捕获,
信用,
general_credit,
void,
verify,
reference_purchase,
purchase_via_preauthorization,
offsite_purchase,
offsite_授权,
3dsecure_purchase,
3dsecure_authorize,
store,
删除,
付款,
reference_authorization
],
credentials:[],
gateway_specific_fields:[],
redacted:false
}
]
}
使用这个结构体时,我可以得到它输出很容易。
类型网关结构{
网关[]结构{
特性[]字符串`json:特性``
CreatedAt字符串`json:created_at`
凭证[] interface {}`json:credentials`
描述interface {}`json:description `
GatewaySpecificFields [] interface {}`json:gateway_specific_fields`
GatewayType字符串`json:gateway_type`
Na我的字符串`json:name`
PaymentMethods []字符串``json:payment_methods`
redacted bool`json:redacted`
国家字符串`json:
令牌字符串`json:令牌`
UpdatedAt字符串`json:updated_at`
}`json:网关
}
但是,只要我将Gateways [] struct分离到它自己的结构中,它就会返回一个空的数组...... b
$ $ $ $ $ $ $ $ $' ] json:features`
CreatedAt string`json:created_at`
Credentials [] interface {}`json:credentials`
描述interface {}`json: description`
GatewaySpecificFields [] interface {}`json:gateway_specific_fields`
GatewayType字符串`json:gateway_type`
名称字符串`json:name`
PaymentMethods [] string`json:payment_methods`
Redacted bool`json:redacted`
状态字符串`json:状态``
令牌字符串`json:令牌
UpdatedAt字符串`json:updated_at`
}
类型网关结构{
Gateways [] gateway`json:网关
}
func ParseResponse(){
var分析的网关
json.Unmarshal(json ,& parsed)
}
你的 ParseResponse
函数存在一个问题,你调用 json.Unmarshal
作为第一个参数传递 json
,这是一个包装名称:这是不明确的。
正如你所看到的,你的代码很好地改变了 ParseResponse
函数。
< code
$ struct $ {
特性[]字符串`json:特性``
CreatedAt字符串`json:created_at`
凭证[] interface {}`json:credentials`
描述接口{} json:description
GatewaySpecificFields [] interface {}`json:gateway_specific_fields`
GatewayType字符串`json:gateway_type`
名称字符串`json: '
PaymentMethods []字符串``json:payment_methods`
Redacted bool`json:redacted`
国家字符串`json:state`
令牌字符串'json:token`
UpdatedAt string`json:updated_at`
}
类型网关struct {
网关[]网关`json:网关`
}
$ b $ func ParseResponse(js [] byte){
var分析的网关
json.Unmarshal(js,& parsed)
fmt.Println(解析)
$ b $ 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
],
状态:保留,
created_at:2016-03-12T18:52:37Z,
updated_at:2016-03-12T18:52:37Z,
name:Spreedly Test,
特征:[
购买,
授权,
捕获,
信用,
general_credit,
void,
verify,
reference_purch ase,
purchase_via_preauthorization,
offsite_purchase,
offsite_authorize,
3dsecure_purchase,
3dsecure_authorize,
store ,
remove,
disburse,
reference_authorization
],
credentials:[],
gateway_specific_fields:[ ],
redacted:false
}
]
}`)
/ *
var分析的网关
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_purchas e 3dsecure_authorize store移除支出reference_authorization] 2016-03-12T18:52:37Z []< nil> [] test Spreedly Test [credit_card sprel third_party_token bank_account apple_pay] false false my_token_here 2016-03-12T18:52:37Z}]}
So, I'm having some trouble parsing this data in golang:
{
"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
}
]
}
When using this struct I can get it to output pretty easily.
type gateways struct {
Gateways []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"`
} `json:"gateways"`
}
But as soon as I seperate the "Gateways []struct" into its own struct then it returns an empty array...
Full source.
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() {
var parsed gateways
json.Unmarshal(json, &parsed)
}
There's a problem with your ParseResponse
function, you're calling json.Unmarshal
passing as first parameter json
, that's a packge name: that's ambiguous.
As you can see, your code works well changing the ParseResponse
function.
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)
}
Outputs:
{[{[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}]}
这篇关于将GoLang中的JSON解析为struct的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!