douhutongvm382381 2015-10-16 17:36
浏览 100
已采纳

Golang:将json字符串解码为来自mysql db的结构

I'm trying to get informations from my db, and one of my fields is actually JSON stored as a string and I would like to get it as a struct.

This is my row's struct :

//there is json flag because I use it to get data from redis too
type InfoClip struct {
 ClipId             string `json:clipId`
 StreamUrl          string `json:streamUrl`
 StartTimeCode      int `json:startTimeCode`
 EndTimeCode        int `json:endTimeCode`
 CreatedAt          string `json:createdAt`
 Metas              string `json:metas` // here I get a string instead of a 'metas' struct
 SourceId           string `json:sourceId`
 ProviderName       string `json:providerName`
 ProviderReference  string `json:providerReference`
 PublicationStatus  string `json:publicationStatus`
 UserId             string `json:userId`
 Name               string `json:name`
 FacebookPage       string `json:facebookPage`
 TwitterHandle      string `json:twitterHandle`
 PermaLinkUrl       string `json:permalinkUrl`
 Logo               string `json:logo`
 Link               string `json:link`
}

This is my metas struct :

type metas struct {
 Title      string `json:title`
 Tags       []string `json:tags`
 categories []string `json:permalink`
}

This is how I'm trying to get this field

func decodeJsonSql (met string) (*metas, error) {
 m := metas{}
 if err := json.Unmarshal([]byte(met), &m); err != nil {
    fmt.Printf("Error decode metas: ", err)
    return nil, err
 } else {
    return &m, err
 }
 } 

func CheckIdSql(mediaId string) (error){
 datab, err := sql.Open("mysql", "tcp()")
 if err != nil {
    fmt.Printf("[SQL ERROR] Cannot Open db => ", err)
    return err
}
if err := datab.Ping(); err != nil {
    fmt.Printf("[SQL ERROR] db connection => ", err)
    return err
}
fmt.Printf("[SQL ONLINE] =>", datab)
defer datab.Close()

q := "SELECT c.id AS clipId, c.streamUrl, c.startTimecode,  c.endTimecode, c.createdAt, s.metas,... FROM clips WHERE c.id = ?"
rows, err := datab.Query(q, mediaId)
if err != nil || err == sql.ErrNoRows {
    fmt.Printf("SQL Err: %s", err)
    return err
}
clips := InfoClip{}
for rows.Next() {
    rows.Scan(&clips.ClipId, &clips.StreamUrl, &clips.StartTimeCode, &clips.EndTimeCode, &clips.CreatedAt, &clips.Metas, ...)
}
ret, err := decodeJsonSql(clips.Metas)
if err != nil{
    return err
}
clips.Metas = ret
fmt.Printf("

[SQL DEBUG RESPONSE]: %v", clips)
return nil
}

But this process is pretty heavy, surely there is an easier way? Thanks.

  • 写回答

1条回答 默认 最新

  • duannai5858 2015-10-17 04:02
    关注

    You can make your metas struct implement the sql.Scanner interface

    It should look something like this:

    func (m *metas) Scan(src interface{}) error {
        strValue, ok := src.(string)
    
        if !ok {
            return fmt.Errorf("metas field must be a string, got %T instead", src)
        }
    
        return json.Unmarshal([]byte(strValue), m)
    }
    

    After that you can use it as an InfoClip field and pass it directly to Scan and drop the decodeJsonSql:

    type InfoClip struct {
        // [...]
        Metas metas `json:metas`
        // [...]
    }
    

    and

    q := "SELECT c.id AS clipId, c.streamUrl, c.startTimecode,  c.endTimecode, c.createdAt, s.metas,... FROM clips WHERE c.id = ?"
    row := datab.QueryRow(q, mediaId)
    clips := InfoClip{}
    err := row.Scan(&clips.ClipId, &clips.StreamUrl, &clips.StartTimeCode, &clips.EndTimeCode, &clips.CreatedAt, &clips.Metas) // [...]
    if err != nil {
        fmt.Printf("SQL Err: %s", err)
        return err
    }
    

    (BTW, as you can see, I replaced datab.Query with datab.QueryRow as you are expecting only one result)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?