将 GoLang 中的 JSON 解析为结构体

所以,我在 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

    }

]

}


使用这个结构时,我可以很容易地让它输出。


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"` 

}

但是一旦我将“网关 [] 结构”分离到它自己的结构中,它就会返回一个空数组......


慕无忌1623718
浏览 190回答 2
2回答

喵喔喔

您的ParseResponse函数存在问题,您正在调用json.Unmarshal作为第一个参数传递json,这是一个包名称:这是模棱两可的。如您所见,您的代码在更改ParseResponse函数时运行良好。package mainimport (&nbsp; &nbsp; "encoding/json"&nbsp; &nbsp; "fmt")type gateway struct {&nbsp; &nbsp; Characteristics&nbsp; &nbsp; &nbsp; &nbsp;[]string&nbsp; &nbsp; &nbsp; `json:"characteristics"`&nbsp; &nbsp; CreatedAt&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string&nbsp; &nbsp; &nbsp; &nbsp; `json:"created_at"`&nbsp; &nbsp; Credentials&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;[]interface{} `json:"credentials"`&nbsp; &nbsp; Description&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;interface{}&nbsp; &nbsp;`json:"description"`&nbsp; &nbsp; GatewaySpecificFields []interface{} `json:"gateway_specific_fields"`&nbsp; &nbsp; GatewayType&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string&nbsp; &nbsp; &nbsp; &nbsp; `json:"gateway_type"`&nbsp; &nbsp; Name&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string&nbsp; &nbsp; &nbsp; &nbsp; `json:"name"`&nbsp; &nbsp; PaymentMethods&nbsp; &nbsp; &nbsp; &nbsp; []string&nbsp; &nbsp; &nbsp; `json:"payment_methods"`&nbsp; &nbsp; Redacted&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; bool&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; `json:"redacted"`&nbsp; &nbsp; State&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string&nbsp; &nbsp; &nbsp; &nbsp; `json:"state"`&nbsp; &nbsp; Token&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string&nbsp; &nbsp; &nbsp; &nbsp; `json:"token"`&nbsp; &nbsp; UpdatedAt&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;string&nbsp; &nbsp; &nbsp; &nbsp; `json:"updated_at"`}type gateways struct {&nbsp; &nbsp; Gateways []gateway `json:"gateways"`}func ParseResponse(js []byte) {&nbsp; &nbsp; var parsed gateways&nbsp; &nbsp; json.Unmarshal(js, &parsed)&nbsp; &nbsp; fmt.Println(parsed)}func main() {&nbsp; &nbsp; var js []byte = []byte(`{"gateways": [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "token": "my_token_here",&nbsp; &nbsp; &nbsp; &nbsp; "gateway_type": "test",&nbsp; &nbsp; &nbsp; &nbsp; "description": null,&nbsp; &nbsp; &nbsp; &nbsp; "payment_methods": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "credit_card",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "sprel",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "third_party_token",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "bank_account",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "apple_pay"&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; "state": "retained",&nbsp; &nbsp; &nbsp; &nbsp; "created_at": "2016-03-12T18:52:37Z",&nbsp; &nbsp; &nbsp; &nbsp; "updated_at": "2016-03-12T18:52:37Z",&nbsp; &nbsp; &nbsp; &nbsp; "name": "Spreedly Test",&nbsp; &nbsp; &nbsp; &nbsp; "characteristics": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "purchase",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "authorize",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "capture",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "credit",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "general_credit",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "void",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "verify",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "reference_purchase",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "purchase_via_preauthorization",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "offsite_purchase",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "offsite_authorize",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "3dsecure_purchase",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "3dsecure_authorize",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "store",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "remove",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "disburse",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "reference_authorization"&nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; "credentials": [],&nbsp; &nbsp; &nbsp; &nbsp; "gateway_specific_fields": [],&nbsp; &nbsp; &nbsp; &nbsp; "redacted": false&nbsp; &nbsp; }]}`)&nbsp; &nbsp; /*&nbsp; &nbsp; &nbsp; &nbsp; var parsed gateways&nbsp; &nbsp; &nbsp; &nbsp; e := json.Unmarshal(js, &parsed)&nbsp; &nbsp; &nbsp; &nbsp; if e != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(e.Error())&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fmt.Println(parsed)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; */&nbsp; &nbsp; 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}]}

扬帆大鱼

看看http://play.golang.org/p/3xJHBmhuei&nbsp;- 解组到你的第二个网关定义成功完成,所以它一定是你缺少的东西。检查 json.Unmarshal 调用是否返回错误。PS 如果您成功地解组到网关的第一个版本,这可能不是问题,但是您上面给出的 JSON 字符串缺少右括号“}”。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go