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)
- 1 回答
- 0 關注
- 124 瀏覽
添加回答
舉報