登录
首页 >  Golang >  Go问答

接受来自未知来源的数据

来源:stackoverflow

时间:2024-03-15 23:57:31 326浏览 收藏

在处理未知来源数据时,如何判断使用哪个结构来解组响应是一个常见问题。特别是当不同响应之间只有细微差别时,例如一个包含消息数组,另一个包含错误数组。一种解决方法是使用一个公共结构,包含所有可能的字段,然后检查是否存在错误数组或消息数组来确定实际响应类型。这种方法消除了创建多个结构并根据响应类型进行切换的需要,从而简化了代码并提高了灵活性。

问题内容

我正在实施 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"`
}

type authorizedeclinedresponse 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"`
        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"`
}

这是我的问题,要使用哪个结构。我正在考虑尝试一个接口{},然后尝试将其转换为结构?

err := json.Unmarshal(b, &whichStructToUse)
    if err != nil {
        panic(err.Error())
    }

当我不知道要使用哪个结构时,关于如何解组响应有什么建议吗?


正确答案


无论交易是成功还是被拒绝,api 总是给我一个 200 响应代码。

我感受到你的痛苦。

这两个响应之间只有一个区别,成功时有 messages,失败时有 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]接口{}

var result map[string]interface{}
json.Unmarshal([]byte(jsonString), &result)

Demonstration

以上就是《接受来自未知来源的数据》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>