亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

收到未知響應正文

收到未知響應正文

Go
絕地無雙 2023-03-07 14:23:16
我正在實施 Authorize.net 信用卡 API。API 總是給我一個 200 響應代碼,無論交易是成功還是被拒絕。但它為成功的交易提供了一個響應主體,為拒絕的交易提供了不同的響應主體。type AuthorizeApprovedResponse struct {    TransactionResponse struct {        ResponseCode   string `json:"responseCode"`        AuthCode       string `json:"authCode"`        AvsResultCode  string `json:"avsResultCode"`        CvvResultCode  string `json:"cvvResultCode"`        CavvResultCode string `json:"cavvResultCode"`        TransID        string `json:"transId"`        RefTransID     string `json:"refTransID"`        TransHash      string `json:"transHash"`        TestRequest    string `json:"testRequest"`        AccountNumber  string `json:"accountNumber"`        AccountType    string `json:"accountType"`        Messages       []struct {            Code        string `json:"code"`            Description string `json:"description"`        } `json:"messages"`        UserFields []struct {            Name  string `json:"name"`            Value string `json:"value"`        } `json:"userFields"`        TransHashSha2                          string `json:"transHashSha2"`        SupplementalDataQualificationIndicator int    `json:"SupplementalDataQualificationIndicator"`        NetworkTransID                         string `json:"networkTransId"`    } `json:"transactionResponse"`    RefID    string `json:"refId"`    Messages struct {        ResultCode string `json:"resultCode"`        Message    []struct {            Code string `json:"code"`            Text string `json:"text"`        } `json:"message"`    } `json:"messages"`}     這是我的問題,要使用哪個結構。我在考慮嘗試使用 interface{} 然后嘗試將其轉換為結構?err := json.Unmarshal(b, &whichStructToUse)    if err != nil {        panic(err.Error())    }當我不知道要使用哪個結構時,關于如何解組響應的任何建議?
查看完整描述

1 回答

?
哆啦的時光機

TA貢獻1779條經驗 獲得超6個贊

API 總是給我一個 200 響應代碼,無論交易是成功還是被拒絕。


我感覺到你的痛苦。


兩個響應之間只有一個區別,success hasMessages和 failure has Errors。結合它們。


type CommonResponse struct {

    TransactionResponse struct {

        ResponseCode   string `json:"responseCode"`

        AuthCode       string `json:"authCode"`

        AvsResultCode  string `json:"avsResultCode"`

        CvvResultCode  string `json:"cvvResultCode"`

        CavvResultCode string `json:"cavvResultCode"`

        TransID        string `json:"transId"`

        RefTransID     string `json:"refTransID"`

        TransHash      string `json:"transHash"`

        TestRequest    string `json:"testRequest"`

        AccountNumber  string `json:"accountNumber"`

        AccountType    string `json:"accountType"`

        Messages       []struct {

            Code        string `json:"code"`

            Description string `json:"description"`

        } `json:"messages"`

        Errors         []struct {

            ErrorCode string `json:"errorCode"`

            ErrorText string `json:"errorText"`

        } `json:"errors"`

        UserFields []struct {

            Name  string `json:"name"`

            Value string `json:"value"`

        } `json:"userFields"`

        TransHashSha2                          string `json:"transHashSha2"`

        SupplementalDataQualificationIndicator int    `json:"SupplementalDataQualificationIndicator"`

        NetworkTransID                         string `json:"networkTransId"`

    } `json:"transactionResponse"`

    RefID    string `json:"refId"`

    Messages struct {

        ResultCode string `json:"resultCode"`

        Message    []struct {

            Code string `json:"code"`

            Text string `json:"text"`

        } `json:"message"`

    } `json:"messages"`

}

然后使用它來解組并檢查錯誤。


    var response CommonResponse;

    json.Unmarshal([]byte(jsonString), &response)

    if len(response.Error) == 0 {

        fmt.Println("Success!")

    } else {

        fmt.Println("Error!")

    }

對于更一般的情況,您可以解組為map[string]interface{}.


var result map[string]interface{}

json.Unmarshal([]byte(jsonString), &result)


查看完整回答
反對 回復 2023-03-07
  • 1 回答
  • 0 關注
  • 124 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號