json - 接口(interface)转换 : interface {} is int64, 不是 []uint8

标签 json go interface nested httpresponse

我正在尝试实现一个可以处理 http 请求并在嵌套 JSON 中发送响应的 go 程序。当我运行我的代码并调用 URL 时,出现运行时错误,这是什么意思?我该如何处理?

panic serving 192.168.0.101:50760: interface conversion: interface {} is int64, not []uint8
goroutine 5 [running]

这是我的函数代码,它在点击 url 时被调用

func logInPass(res http.ResponseWriter, req *http.Request) {
    type Resp struct {
        Result []map[string]interface{} `json:"Result,omitempty"`
        Status string                   `json:"Status"`
    }
    type AxleUser struct {
        Mobile   string `json:"Mobile"`
        Password string `json:"Password"`
    }

    var Response Resp
    Response.Status = "failed"
    Result := make(map[string]interface{})
    db, err := sql.Open("mysql", "root:chikkIbuddI57@tcp(127.0.0.1:3306)/b2b")
    if err != nil {
        panic(err.Error())
    }
    defer db.Close()

    rnd := render.New()
    b, err := ioutil.ReadAll(req.Body)
    defer req.Body.Close()

    if err != nil {
        panic(err.Error())
    }
    // Unmarshal the request body
    var msg AxleUser
    err = json.Unmarshal(b, &msg)
    if err != nil {
        panic(err.Error())
    }
    // get shop id from emp table using mobile number and password
    userrows, usererr := db.Query("SELECT b2b_emp_id,b2b_shop_id,b2b_shop_name,b2b_emp_name,b2b_emp_mobile_number FROM b2b_employee_tbl WHERE b2b_emp_mobile_number=? and b2b_password=?", msg.Mobile, msg.Password)
    if usererr != nil {
        panic(usererr.Error())
    }
    usercolumns, usererr := userrows.Columns()
    if usererr != nil {
        panic(usererr.Error())
    }

    usercount := len(usercolumns)
    values := make([]interface{}, usercount)
    scanArgs := make([]interface{}, usercount)
    for i := range values {
        scanArgs[i] = &values[i]
    }
    for userrows.Next() {
        usererr := userrows.Scan(scanArgs...)
        if usererr != nil {
            panic(usererr.Error())
        }
        for i, v := range values {
            if v != nil {
                Result[usercolumns[i]] = fmt.Sprintf("%s", string(v.([]byte)))
            }
        }
        Response.Result = append(Response.Result, Result)
        Response.Status = "success"
    }

    res.Header().Set("Content-Type", "application/json")
    rnd.JSON(res, http.StatusOK, Response)
}

提前致谢!

最佳答案

我改变了这一行

values := make([]interface{}, usercount)

values := make([]string, usercount)

还有这一行

Result[usercolumns[i]] = fmt.Sprintf("%s", string(v.([]byte)))

Result[usercolumns[i]] = v

关于json - 接口(interface)转换 : interface {} is int64, 不是 []uint8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49068556/

相关文章:

python - 将json dict转换为pandas df中的行

json - python 3 : deserialize nested dictionaries from sqlite

python - 为什么 PyGtk 中的 set_model 方法将第一列的值复制到第三列?

amazon-web-services - base64 编码 io.Reader

c# - 使用 C# 的 OOPS 查询

go - 如何避免 golang 函数在调用嵌入类型和嵌入类型之间具有不同的行为?

arrays - 在字典的数组中加载带有字典的 JSON 文件

c# - 获取Response后Json解析错误

go - 打印给定索引的字符串的排列

image - 使用 go 图像库从标准输出解码 bmp 图像