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

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

Unmarshal 從 postgresql 獲取的對象的 Json 數(shù)組

Unmarshal 從 postgresql 獲取的對象的 Json 數(shù)組

Go
翻閱古今 2023-07-31 16:30:45
我在 Postgres 中有一個 Jsonb 表Create Table Business(    id serial not null primary key,    id_category integer not null,    name varchar(50) not null,    owner varchar(200) not null,    coordinates jsonb not null,    reason varchar(300) not null,    foreign key(id_category) references Category(id));正如你所看到的,我將坐標存儲為jsonb前任:Insert into Business(id_category, name, owner, coordinates, reason) values (1,'MyName','Owner', [{"latitude": 12.1268142, "longitude": -86.2754}]','Description')我提取數(shù)據(jù)并分配它的方式是這樣的。type Business struct {    ID int `json:"id,omitempty"`    Name string `json:"name,omitempty"`    Owner string `json:"owner,omitempty"`    Category string `json:"category,omitempty"`    Departments []string `json:"departments,omitempty"`    Location []Coordinates `json:"location,omitempty"`    Reason string `json:"reason,omitempty"`}type Coordinates struct {    Latitude float64 `json:"latitude,omitempty"`    Longitude float64 `json:"longitude,omitempty"`}func (a Coordinates) Value() (driver.Value, error) {    return json.Marshal(a)}func (a *Coordinates) Scan(value []interface{}) error {    b, ok := value.([]byte)    if !ok {        return errors.New("type assertion to []byte failed")    }    return json.Unmarshal(b, &a)}然而,我不斷收到這樣的消息。sql:列索引 3 上的掃描錯誤,名稱“坐標”:不支持的掃描,將 driver.Value 類型 []uint8 存儲到類型 *models.Cooperatives 中誰能告訴我如何解決這個問題?檢索對象的 json 數(shù)組并將其分配給 Business 內(nèi)的坐標字段。
查看完整描述

2 回答

?
拉丁的傳說

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

1.

正如您從文檔中看到的Scanner,要滿足該接口,需要該方法

Scan(src?interface{})?error

但你的*Coordinates類型實現(xiàn)了不同的方法

Scan(value?[]interface{})?error

類型interface{}[]interface{}是兩個截然不同的東西。

2.

Scanner 接口必須在要作為參數(shù)傳遞給 的字段類型上實現(xiàn)rows.Scan。也就是說,您已經(jīng)Scan在 上實現(xiàn)了您的方法*Coordinates,但 Location 字段的類型是[]Coordinates

同樣,類型*Coordinates和類型[]Coordinates是兩個非常不同的東西。


因此,解決方案是在正確的類型上正確實現(xiàn)接口。

請注意,由于 Go 不允許向未命名類型添加方法,并且[]Coordinates?未命名類型,因此您需要聲明一個新類型,然后使用它來代替[]Coordinates.

type CoordinatesSlice []Coordinates


func (s *CoordinatesSlice) Scan(src interface{}) error {

? ? switch v := src.(type) {

? ? case []byte:

? ? ? ? return json.Unmarshal(v, s)

? ? case string:

? ? ? ? return json.Unmarshal([]byte(v), s)

? ? }

? ? return errors.New("type assertion failed")

}


// ...


type Business struct {

? ? // ...

? ? Location CoordinatesSlice `json:"location,omitempty"`

? ? // ...

}

筆記

如果業(yè)務位置始終只有對坐標作為 jsonb 對象存儲到數(shù)據(jù)庫中,并將類型Location從更改CoordinatesSliceCoordinates,相應地將Scanner實現(xiàn)從移動*CoordinatesSlice*Coordinates。


查看完整回答
反對 回復 2023-07-31
?
三國紛爭

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

我知道這個解決方案確實沒有優(yōu)化,但這是唯一有效的方法。


基本上我必須獲取 json,然后對 Location 屬性進行解組。


var location string = ""


if err := json.Unmarshal([]byte(location), &business.Location); err != nil { panic(err) }



查看完整回答
反對 回復 2023-07-31
  • 2 回答
  • 0 關注
  • 154 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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