登录
首页 >  Golang >  Go问答

JSON解码器的重复调用是由于响应结构微小变化引起的

来源:stackoverflow

时间:2024-02-13 17:51:26 413浏览 收藏

在Golang实战开发的过程中,我们经常会遇到一些这样那样的问题,然后要卡好半天,等问题解决了才发现原来一些细节知识点还是没有掌握好。今天golang学习网就整理分享《JSON解码器的重复调用是由于响应结构微小变化引起的》,聊聊,希望可以帮助到正在努力赚钱的你。

问题内容

我已将 github 和 google 身份验证系统添加到我的 web 应用程序中。我希望在这两种情况下都能获得用户电子邮件。我尝试创建一个函数来发出 api 请求并获取电子邮件。

google 返回 json 对象github 返回 json 数组 作为响应时,我遇到了问题。

我想不出如何避免调用 json 解码器两次的方法,因为我不能为它们提供相同的类型变量。

// Sends a request to the API and
// authorizes it by setting HTTP header "Authorization" to authHeader value
func getUserEmail(endpoint, authHeader, provider string) (string, error) {
    var email string       // Store user email here
    var client http.Client // Create client so we can modify request headers

    // Create a GET request to the endpoint
    req, err := http.NewRequest("GET", endpoint, nil)
    if err != nil {
        return "", err
    }

    // Authorize the request by using the HTTP header
    req.Header.Add("Authorization", authHeader)

    // Give the data back as JSON
    req.Header.Add("accept", "application/json")

    // Send the request
    resp, err := client.Do(req)
    if err != nil {
        fmt.Println("Internet connection or client policy error")
        return "", err
    }
    defer resp.Body.Close()

    if provider == "google" {
        var response map[string]interface{}

        err = json.NewDecoder(resp.Body).Decode(&response)
        if err != nil {
            fmt.Println("Error occured during decoding access token response")
            return "", err
        }

        email = response["email"].(string)

    } else if provider == "github" {
        var response []map[string]interface{}

        err = json.NewDecoder(resp.Body).Decode(&response)
        if err != nil {
            fmt.Println("Error occured during decoding access token response")
            return "", err
        }

        email = response[0]["email"].(string)
    } else {
        return "", errors.New("invalid provider")

    }

    return email, nil
}

正确答案


您可以解组到 var 响应接口{}。解组 json 后,您可以在 response 上执行 type 断言 来检查它是否是 []interface{}map[string]interface{} 并从那里开始。

var email string
var response interface{}
if err := json.newdecoder(r.body).decode(&response); err != nil {
    return err
}

// if you are sure that the structure of the content of the response,
// given its type, is always what you expect it to be, you can use a
// quick-and-dirty type switch/assertion.
switch v := response.(type) {
case []interface{}:
    email = v[0].(map[string]interface{})["email"].(string)
case map[string]interface{}:
    email = v["email"].(string)
}

// but! if you're not sure, if the apis don't provide a guarantee,
// then you should guard against panics using the comma-ok idiom
// at every step.
if s, ok := response.([]interface{}); ok && len(s) > 0 {
    if m, ok := s[0].(map[string]interface{}); ok && len(m) > 0 {
        email, _ = m["email"].(string)
    }
} else if m, ok := response.(map[string]interface{}); ok && len(m) > 0 {
    email, _ = m["email"].(string)
}

您还可以根据提供程序值预先分配一个指向预期类型的​​指针,并将请求正文解组到其中,这将减少所需的类型断言数量,但需要指针取消引用.

var email string
var response interface{}
if provider == "google" {
    response = new(map[string]interface{})
} else if provider == "github" {
    response = new([]map[string]interface{})
}
if err := json.NewDecoder(r.Body).Decode(response); err != nil {
    return err
}

switch v := response.(type) {
case *[]map[string]interface{}:
    email = (*v)[0]["email"].(string) // no need to assert the slice element's type
case *map[string]interface{}:
    email = (*v)["email"].(string)
}

文中关于的知识介绍,希望对你的学习有所帮助!若是受益匪浅,那就动动鼠标收藏这篇《JSON解码器的重复调用是由于响应结构微小变化引起的》文章吧,也可关注golang学习网公众号了解相关技术文章。

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