将struct slice插入Postgresql jsonb []

时间:2017-12-08 14:04:08

标签: json postgresql go

我有一个结构切片,我正试图编组成一个json数组,用于插入到postgresql数据库中,列类型为jsonb []。

type t struct {
    A int
    B string
}
a := t{1, "hello"}
b := t{2, "goodbye"}

var c []t
c = append(c, a, b)

bytes, _ := json.Marshal(c)

stmt := `INSERT INTO test (s, ja) VALUES ($1, $2)`
_, err = db.Exec(stmt, "test", bytes)
if err != nil {
    log.Fatal("Insert error:", err)
}

我收到错误:

  

pq:格式错误的数组文字:“[{”A“:1,”B“:”hello“},{”A“:2,”B“:”goodbye“}]”

如何使数据适合jsonb []列?

0 个答案:

没有答案
相关问题