为了账号安全,请及时绑定邮箱和手机立即绑定

go "encoding/json" : marshal json 字段

go "encoding/json" : marshal json 字段

Go
潇湘沐 2021-10-25 20:19:05
我有一个带有 json 字段(DisplayInfo 和 FormatInfo)的 PostgreSQL 模式。该领域的结构是动态的。我只能读取和渲染它作为字符串(渲染结构中的字符串类型):[ {    "ID":9,  "Name":"120 №1",  "DisplayInfo":"{\"path\": \"http://path/to/img.png\"}",  "Format":{       "Code":"frame-120",     "Width":120,     "Height":60,     "FormatInfo":"[{\"name\": \"\\u0413\\u043b\\u0430\\u0432\\u043d\\u043e\\u0435 \\u0438\\u0437\\u043e\\u0431\\u0440\\u0430\\u0436\\u0435\\u043d\\u0438\\u0435\", \"field_type\": \"img\", \"key\": \"path\"}]"  },  "Weight":0.075,  "Application":8,  "Url":"//path/to/game",  "Referrer":"" }]但我希望输出字段 DisplayInfo 作为 JSON 对象。如何 ?我的渲染代码:func renderJSON(w http.ResponseWriter, obj models.Model) {    js, err := json.Marshal(obj)    if err != nil {        http.Error(w, err.Error(), http.StatusInternalServerError)        return    }    w.Header().Set("Content-Type", "application/json; charset=utf-8")    w.Header().Set("Access-Control-Allow-Origin", "*")    w.Write(js)}UPD 1:该字段的结构是动态的。DisplayInfo 可能有“路径”字段,也可能没有。他们可能有额外的字段。UPD 2. 我想输出 DisplayInfo 和 FormatInfo 作为 json-object(not string),作为整个 object 的一部分,像这样:[ {    "ID":9,  "Name":"120 №1",  "DisplayInfo":{"path": "http://path/to/img.png"},  "Format":{       "Code":"frame-120",     "Width":120,     "Height":60,     "FormatInfo":[{"name": "\\u0413\\u043b\\u0430\\u0432\\u043d\\u043e\\u0435 \\u0438\\u0437\\u043e\\u0431\\u0440\\u0430\\u0436\\u0435\\u043d\\u0438\\u0435", "field_type": "img", "key": "path"}]  },  "Weight":0.075,  "Application":8,  "Url":"//path/to/game",  "Referrer":"" }]UPD 3:结构实际结构是:type BannerSerializer struct {    ID          int    Name        string    DisplayInfo string    Format      formatSerializer    Weight      float32    Application int    Url         string    Referrer    string}  DisplayInfo 序列化为 base64 字符串(或者像 base64,不知道)
查看完整描述

3 回答

  • 3 回答
  • 0 关注
  • 203 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信