zoukankan      html  css  js  c++  java
  • golang 结构体内嵌结构体序列化填充

    看我上一篇随笔 https://www.cnblogs.com/zengxm/p/13473340.html


    其中使用mysql原始语句查询结果并返回

    上一篇随笔代码

    并想直接映射到结构体中,后来发现结构体序列化时有个坑

    这次实际操作了一下解决了

    代码如下

    type Curriculums struct {
        CID         int            `gorm:"column:c_id" json:"cid"`
        UID         int            `gorm:"column:u_id" json:"uid"`
        //TID         int            `gorm:"column:t_id" json:"tid"`
        Name         string        `gorm:"column:c_name" json:"title"`
        Price         float64        `gorm:"column:price" json:"price"`
        Info        string        `gorm:"column:info" json:"info"`
        Image        string        `gorm:"column:c_image" json:"img"`
        CreateTime     *time.Time    `gorm:"column:create_at" json:"at"`
        DeleteTime     *time.Time    `gorm:"column:delete_at" json:"et"`
    
        AdminDelTime      *time.Time        `gorm:"column:admin_del" json:"a_del"`        // 后台人员删除时间
        Aid               int                `gorm:"column:a_id" json:"aid"`                // 后台执行人信息
    }
    func (cr *Curriculums)TableName()string{
        return "curriculums"
    }
    type User struct {
        ID             uint64        `gorm:"column:u_id" json:"uid,omitempty"`
        Nickename     string        `gorm:"column:nickename" json:"nick,omitempty"`
        Username     string        `gorm:"column:username" json:"uname,omitempty"`
        Pswd         string        `gorm:"column:pswd" json:"pswd,omitempty"`
        Status         int            `gorm:"column:status" json:"st,omitempty"`
        RID         int            `gorm:"column:r_id" json:"rid,omitempty"`
        Portrait     string        `gorm:"column:portrait" json:"img,omitempty"`
        CreateTime     *time.Time    `gorm:"column:create_at" json:"at,omitempty"`
    
        AdminDelTime      *time.Time        `gorm:"column:admin_del" json:"a_del,omitempty"`        // 后台人员删除时间
        Aid               int                `gorm:"column:a_id" json:"aid,omitempty"`                // 后台执行人信息
    }
    func (u *User)TableName()string{
        return "users"
    }
    
    type SelectData struct {
        User
        JsonStr string   `gorm:"column:datas" json:"datas,omitempty"`
        MarshalerData []Curriculums        `json:"datas,omitempty" `
    }
    
    
    
    
    func main(){
        var result []SelectData
    
        SQL := "SELECT u.u_id,u.nickename,u.username, " +
            "CONCAT("[",GROUP_CONCAT('{','"cid":',c.c_id,',"title":"',c.c_name,'","price":',c.price,',"info":"',c.info,'"}'),"]") as datas " +
            "FROM users AS u " +
            "JOIN curriculums AS c " +
            "ON c.u_id = u.u_id " +
            "WHERE c.c_name like '%o%' GROUP BY u.u_id"
    
        conn.Raw(SQL).Scan(&result)
    
        for index,_ := range result{
    
            result[index].JsonStr = strings.Replace(result[index].JsonStr,"
    ","",-1)
    
            //fmt.Println(result[index])
            /*
            {{100000 acfun 7745742641  0 0  <nil> <nil> 0} [{"cid":102,"title":"python 学习","price":20.00,"info":"python "},{"cid":103,"title":"golang 学习","price":70.79,"info":"go语言 gin框架"}] {[]}}
            {{100006 giligililllsa666 z99999es  0 0  <nil> <nil> 0} [{"cid":117,"title":"测试保存视频,视频url保存在阿里云oss上","price":9.99,"info":"test in"}] {[]}}
            */
            b,err := json.Marshal(result[index])
            if err != nil {
                panic(err)
            }
    
            fmt.Println(string(b))
            /*
    上面输出与序列化对比丢失了 datas 数据 {"uid":100000,"nick":"acfun","uname":"7745742641"} {"uid":100006,"nick":"giligililllsa666","uname":"z99999es"}
    */ } }

    后来经过排查发现是结构体中内嵌结构体并被命名后产生的坑

    type SelectData struct {
        User
        JsonStr string   `gorm:"column:datas" json:"datas,omitempty"`
        MarshalerData []Curriculums        `json:"datas,omitempty" `   # 内嵌结构体被命名了,被命名内嵌结构体如果不指定该命名则不会序列化
        
    }

    避免产生则需要添加一个中间结构体,修改如下

    type Mid struct {
        d []Curriculums        // 小写避免被序列化
    }
    
    type SelectData struct {
        User
        JsonStr string   `gorm:"column:datas" json:"datas,omitempty"`   
        //MarshalerData []Curriculums        `json:"datas,omitempty" `
        Mid
    }
    success:
          {"uid":100000,"nick":"acfun","uname":"7745742641","datas":"[{"cid":102,"title":"python 学习","price":20.00,"info":"python "},{"cid":103,"title":"golang 学习","price":70.79,"info":"go语言 gin框架"}]"}
          {"uid":100006,"nick":"giligililllsa666","uname":"z99999es","datas":"[{"cid":117,"title":"测试保存视频,视频url保存在阿里云oss上
    ","price":9.99,"info":"test in"}]"}

    下次记得使用原始sql语句查询结果并序列化是对内嵌结构体需要注意~~~~~~~~~~~~~~~~

    参考资料:

    https://www.cnblogs.com/staff/p/13223443.html

    https://www.it1352.com/809480.html (重点)

    -----------------------------------------------------------分割线-------------------------------------------------------------------

    本笔记自用 而已,如有错误请指正!

  • 相关阅读:
    Atitit js nodejs下的进程管理wmic process进程管理
    Atitit 提取sfit特征点,并绘制到原图上
    Atitit 局部图查找大图 方法 与 说明
    Atitit java读取tif文件为空null的解决 图像处理
    Aititi 特征点检测算法与匹配的前世今生与历史传承attilax总结v4
    Atitit it行业图像处理行业软件行业感到到迷茫的三大原因和解决方案
    Atitit js nodejs 图像处理压缩缩放算法 attilax总结
    Atitit 2017年第68界机器视觉图像处理学术大会会议记要attilax总结自建学院自颁学位理论
    Atitit nodejs js 获取图像分辨率 尺寸 大小 宽度 高度
    Atitit 图像处理之编程之类库调用的接口api cli gui ws rest  attilax大总结.docx
  • 原文地址:https://www.cnblogs.com/zengxm/p/13488448.html
Copyright © 2011-2022 走看看