2019-01-12 16:42
浏览 123

如何将Postgresql JSON类型转换为Golang本机?

I am trying to convert a postgres column that is of type json to golang json object.

type MY_JSON struct {
  MY_ID string `json:"my_id"`
  MY_INFO []MY_INNER_JSON `json:"my_info"`

type MY_INNER_JSON struct {
    SOME_ID   string `json:"some_id"`
    SOME_NUM  int64 `json:"some_num"`
    SOME_OPTIONAL  string `json:"some_optional,omitempty"`

rows, err := db.Query("SELECT my_json FROM my_json_table LIMIT 1;")

for rows.Next() {
    var mycolumn MY_JSON
    err = rows.Scan(&mycolumn)

This is what the json looks like

    "my_id": "this is my_id",
    "my_info": [
            "some_id": "some_id",
            "some_num": 123
            "some_id": "some_id",
            "some_num": 123,
            "some_optional": "sometimes more"

I am getting panic from golang during runtime.

Can someone point me a direction and tell me where I am doing wrong?

1条回答 默认 最新

相关推荐 更多相似问题