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 (重点)

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

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

  • 相关阅读:
    java相关知识集锦
    adb相关基础知识集锦
    我所理解的OOP——UML六种关系
    据说每个大牛、小牛都应该有自己的库——DOM处理续
    据说每个大牛、小牛都应该有自己的库——DOM处理
    JavaScript 继承
    据说每个大牛、小牛都应该有自己的库——Event处理
    据说每个大牛、小牛都应该有自己的库——框架篇
    可拖动的DIV续
    CSS hack前传——背景图片全屏
  • 原文地址:https://www.cnblogs.com/zengxm/p/13488448.html
Copyright © 2011-2022 走看看