我在 mysql 中有一個表,其中包含 3 列配置文件字符串、userInformation JSON、徽章字符串。輪廓 用戶信息 徽章https://ps.w.org/metronet-profile-picture/assets/icon-256x256.png?rev=2464419 {"name": "Suzan Collins", "points": 10000, "countryName": "波蘭"} 資產/batcherPage/gold.png這是我的結構:type BatchersData struct { ProfileURL string `json:"profileUrl"` UserInformation UserInformation `json:"userInformation"` Badge string `json:"badge"`}type UserInformation struct { Points int64 `json:"points"` Name string `json:"name"` CountryName string `json:"countryName"`}我想要做的是在此表上進行選擇查詢,即 GET 并檢索所有信息..使用此代碼,我訪問了 Profile 和 Badge :func getBatchers(c *gin.Context) { var batchers []BatchersData rows, err := db.Query("SELECT profileUrl, badge FROM Batchers_page_db") if err != nil { return } defer rows.Close() for rows.Next() { var batcher BatchersData if err := rows.Scan(&batcher.ProfileURL, &batcher.Badge); err != nil { return } batchers = append(batchers, batcher) } if err := rows.Err(); err != nil { return } c.IndentedJSON(http.StatusOK, batchers)}但我也想訪問 JSON 列,即 UserInformation。我知道查詢將是rows, err := db.Query("SELECT * FROM Batchers_page_db")但我必須更改此聲明if err := rows.Scan(&batcher.ProfileURL, &batcher.Badge);我試過這樣做:但沒有任何效果rows.Scan(&batcher.ProfileURL,&batcher.UserInformation, &batcher.Badge);
2 回答

千萬里不及你
TA貢獻1784條經驗 獲得超9個贊
從 Batchers_page_db 中選擇 JSON_EXTRACT(userInformation,'$.name') 作為 profileName, JSON_EXTRACT(userInformation,'$.points') 作為 userPoints
未經測試,但類似這樣的方法會起作用,但請確保您的數據庫字段必須是 JSON 類型。
- 2 回答
- 0 關注
- 218 瀏覽
添加回答
舉報
0/150
提交
取消