我的问题是

用grom 自定义字段,怎么解析mysql 字符串类型的json 数组

type WarehouseCheckGoods struct {
    ID               int64     `gorm:"column:id" db:"column:id" json:"id" form:"id"`
    GoodsId          int64     `gorm:"column:goods_id" db:"column:goods_id" json:"goods_id" form:"goods_id"`
    SkuCode          string    `gorm:"column:sku_code" db:"column:sku_code" json:"sku_code" form:"sku_code"`
    Remarks          string    `gorm:"column:remarks" db:"column:remarks" json:"remarks" form:"remarks"`
    RealNumber       int64     `gorm:"column:real_number" db:"column:real_number" json:"real_number" form:"real_number"`
    BookNumber       int64     `gorm:"column:book_number" db:"column:book_number" json:"book_number" form:"book_number"`
    AdjustNumber     int64     `gorm:"column:adjust_number" db:"column:adjust_number" json:"adjust_number" form:"adjust_number"`
    WarehouseCheckId int64     `gorm:"column:warehouse_check_id" db:"column:warehouse_check_id" json:"warehouse_check_id" form:"warehouse_check_id"`
    ThreeWarehouse   AutoGenerated   `gorm:"column:three_warehouse" db:"column:three_warehouse" json:"three_warehouse" form:"three_warehouse"`
    CreatedAt        time.Time `gorm:"column:created_at" db:"column:created_at" json:"created_at" form:"created_at"`
    UpdatedAt        time.Time `gorm:"column:updated_at" db:"column:updated_at" json:"updated_at" form:"updated_at"`
}

type AutoGenerated []struct {
    WarehouseID  int    `json:"warehouse_id"`
    BookNumber   string `json:"book_number"`
    AdjustNumber string `json:"adjust_number"`
}

func (j *AutoGenerated) Scan(value interface{}) error {
    bytes, _ := value.([]uint8)
    var lins AutoGenerated
    json.Unmarshal([]byte(bytes), &lins)
//打印lins ,输出正常
    j = &lins
// 把值赋给 指针,输出是null
    return nil
}

func (j AutoGenerated) Value() (driver.Value, error) {
    return j, nil
}

其中ThreeWarehouse 这个字段为json 数组格式的字符 格式

[{"warehouse_id":1,"book_number":"0","adjust_number":"15"}]

然后我自定义的类型AutoGenerated,代码如上方,我把json绑定到结构体,打印都正常,但是赋值给指针,后接口请求不正常;哪位大神知道下,我这种怎么处理,谢谢🙏

目前返回的数据格式是

{
        "id": 3,
        "code": "PD1648024354793825",
        "name": "",
        "warehouse_id": 1,
        "state": 1,
        "company_id": 1092,
        "user_id": 1,
        "created_at": "2022-03-23T16:32:34+08:00",
        "updated_at": "2022-03-23T16:54:30+08:00",
        "deleted_at": "2022-03-23T16:54:30+08:00",
        "CheckGoods": [
            {
                "id": 3,
                "goods_id": 1,
                "sku_code": "164802272092095",
                "remarks": "",
                "real_number": 2580,
                "book_number": 3000,
                "adjust_number": -420,
                "warehouse_check_id": 3,
                "three_warehouse": null, 
                "created_at": "2022-03-23T16:32:34+08:00",
                "updated_at": "2022-03-23T16:32:34+08:00"
            }
        ]
    }]
}

可以看出three_warehouse,这个字段返回的是null

我期望的是

{
        "id": 3,
        "code": "PD1648024354793825",
        "name": "",
        "warehouse_id": 1,
        "state": 1,
        "company_id": 1092,
        "user_id": 1,
        "created_at": "2022-03-23T16:32:34+08:00",
        "updated_at": "2022-03-23T16:54:30+08:00",
        "deleted_at": "2022-03-23T16:54:30+08:00",
        "CheckGoods": [
            {
                "id": 3,
                "goods_id": 1,
                "sku_code": "164802272092095",
                "remarks": "",
                "real_number": 2580,
                "book_number": 3000,
                "adjust_number": -420,
                "warehouse_check_id": 3,
                "three_warehouse": [
                          {"warehouse_id":1,"book_number":"0","adjust_number":"15"}
                         ], 
                "created_at": "2022-03-23T16:32:34+08:00",
                "updated_at": "2022-03-23T16:32:34+08:00"
            }
        ]
    }]
}

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: a631807682

https://gorm.io/docs/data_types.html

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: li-jin-gou

@sonhineboy 现在 gorm 支持了 serializer,内置了json解析功能,可以使用 tag 就可以解析 json 了 。 具体参考 https://github.com/go-gorm/gorm/blob/master/tests/serializer_test.go 的示例代码