为了账号安全,请及时绑定邮箱和手机立即绑定

返回值时映射文字中缺少键

返回值时映射文字中缺少键

Go
慕少森 2023-08-14 17:37:20
我试图在 html 上显示一个 json 数组,但我需要在 var 之前添加一个名称:func main() {    r := gin.Default()    r.GET("/ping", func(c *gin.Context) {        var mjson []model.Author        db, err := sql.Open("mysql", "root:@tcp(localhost)/bibliotecagnommo")        if err != nil {            fmt.Println(err)        }        datos, err2 := db.Query("SELECT * FROM author;")        if err2 != nil {            fmt.Println(err2)        }        for datos.Next() {            var id, nombre, apellido string            err3 := datos.Scan(&id, &nombre, &apellido)            if err3 != nil {                fmt.Println(err3)            }            autor := new(model.Author)            autor.Id = id            autor.FirstName = nombre            autor.LastName = apellido            //autorJsonString := string(autorJson);            mjson = append(mjson, *autor)        }        c.JSON(200, gin.H{    //This line of code     "wordToAvoid":mjson,        })    })    r.Run()}有没有办法避免在前面加上一个词?因为当我在 html 上打印 var 时,它会显示我输入的单词。
查看完整描述

1 回答

?
MM们

TA贡献1886条经验 获得超2个赞

只需更换

c.JSON(200, gin.H{ 
   //This line of code     "wordToAvoid":mjson,
        })

c.JSON(200, mjson)

注意:gin.H具有 type map[string]interface{},因此您需要传递一个映射,即那里必须有一个键。但如果你看到,方法 JSON 接受intinterface{},所以只需传递一个接口{},即你的对象mjson


查看完整回答
反对 回复 2023-08-14
  • 1 回答
  • 0 关注
  • 79 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信