第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

將 GoLang 中的 JSON 解析為結(jié)構(gòu)體

將 GoLang 中的 JSON 解析為結(jié)構(gòu)體

Go
慕無忌1623718 2021-12-20 17:11:17
所以,我在 golang 中解析這些數(shù)據(jù)時(shí)遇到了一些麻煩:{"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    }]}使用這個(gè)結(jié)構(gòu)時(shí),我可以很容易地讓它輸出。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"` }但是一旦我將“網(wǎng)關(guān) [] 結(jié)構(gòu)”分離到它自己的結(jié)構(gòu)中,它就會(huì)返回一個(gè)空數(shù)組......
查看完整描述

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}]}



查看完整回答
反對(duì) 回復(fù) 2021-12-20
?
揚(yáng)帆大魚

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)“}”。


查看完整回答
反對(duì) 回復(fù) 2021-12-20
  • 2 回答
  • 0 關(guān)注
  • 228 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)