第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

SQLC - 如何檢索存儲為 JSONB 的數(shù)據(jù)

SQLC - 如何檢索存儲為 JSONB 的數(shù)據(jù)

Go
翻閱古今 2023-03-07 16:37:28
我有一個包含字段 JSONB 的簡單表:CREATE TABLE IF NOT EXISTS "test_table" (    "id" text NOT NULL,    "user_id" text NOT NULL,    "content" jsonb NOT NULL,    "create_time" timestamptz NOT NULL,    "update_time" timestamptz NOT NULL,    PRIMARY KEY ("id"));我使用一個簡單的查詢來生成帶有 SQLC 的樣板文件。-- name: GetTestData :oneSELECT * FROM test_tableWHERE id = $1 LIMIT 1;但是該content屬性生成為json.RawMessage.type TestTable struct {    ID          string          `json:"id"`   UserId      string          `json:"user_id"`    Content     json.RawMessage `json:"content"`    CreateTime  time.Time       `json:"create_time"`    UpdateTime  time.Time       `json:"update_time"`}下面是存儲在內(nèi)容列中的 JSON 示例:{  "static": {    "product": [      {        "id": "string",        "elements": {          "texts": [            {              "id": "string",              "value": "string"            }          ],          "colors": [            {              "id": "string",              "value": "string"            }          ],          "images": [            {              "id": "string",              "values": [                {                  "id": "string",                  "value": "string"                }              ]            }          ]        }      }    ]  },  "dynamic": {    "banner": [      {        "id": "string",        "elements": {          "texts": [            {              "id": "string",              "value": "string"            }          ],          "colors": [            {              "id": "string",              "value": "string"            }          ],          "images": [            {              "id": "string",              "values": [                {                  "id": "string",                  "value": "string"                }              ]            }          ]        }      }    ]  }}Static 或 Dynamic 中的嵌套屬性是數(shù)組。content 屬性應(yīng)該包含一個嵌套對象,我似乎無法提取其中的數(shù)據(jù)。json.Unrmarshall()似乎只獲得頂級屬性。有沒有辦法轉(zhuǎn)換 map[string]interface{}為內(nèi)容或幫助 SQLC 生成屬性作為接口而不是 RawMessage?
查看完整描述

1 回答

?
慕斯王

TA貢獻1864條經(jīng)驗 獲得超2個贊

您的 JSON 包含staticdynamic鍵。您正在解析為 amap[string]json.RawMessage然后嘗試從地圖中檢索Static和檢索Dynamic(注意大寫)。

修復(fù)地圖鍵(即json.Unmarshal(res["static"], &static)),您的代碼可能會工作。更好的解決方案可能是在嘗試解組之前檢查密鑰是否存在。


查看完整回答
反對 回復(fù) 2023-03-07
  • 1 回答
  • 0 關(guān)注
  • 147 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號