首页 > 解决方案 > 从 postgresql 获得的对象的 Unmarshal Json Array

问题描述

我在 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')

我提取数据并分配它的方式是这样的。

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.Coordinates中

我用来提取信息的控制器就是这个。

func (b *BusinessRepoImpl) Select() ([]models.Business, error) {
    business_list := make([]models.Business, 0)
    rows, err := b.Db.Query("SELECT business.id, business.name, business.owner, business.coordinates, business.reason_froggy, category.category FROM business INNER JOIN category on category.id = business.id_category group by business.id, business.name, business.owner, business.coordinates, business.reason_froggy, category.category")
    if err != nil {
        return business_list, err
    }
    for rows.Next() {
        business := models.Business{}
        err := rows.Scan(&business.ID, &business.Name, &business.Owner, &business.Location, &business.Reason, &business.Category)
        if err !=  nil {
            break
        }
        business_list = append(business_list, business)
    }
    err = rows.Err()
    if err != nil {
        return business_list, err
    }
    return business_list, nil
}

谁能告诉我如何解决这个问题?检索对象的 json 数组并将其分配给 Business 中的坐标字段。

标签: postgresqlgo

解决方案


1.

正如您从文档中看到的那样Scanner,要满足接口,需要该方法

Scan(src interface{}) error

但是您的*Coordinates类型实现了不同的方法

Scan(value []interface{}) error

类型interface{}[]interface{}是两个非常不同的东西。

2.

Scanner 接口必须在要作为参数传递给的字段类型上实现rows.Scan。也就是说,您已经实现了您的Scan方法,*Coordinates但 Location 字段的类型是[]Coordinates.

同样,类型*Coordinates[]Coordinates是两个非常不同的东西。


所以解决方案是正确实现接口并在正确的类型上。

请注意,由于 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"`
    // ...
}

笔记

如果业务位置始终只有对坐标作为 jsonb 对象存储到数据库中,并将Location类型从CoordinatesSliceto更改为Coordinates并相应地将Scanner实现从*CoordinatesSliceto移动*Coordinates


推荐阅读