我有一個帶有 json 字段(DisplayInfo 和 FormatInfo)的 PostgreSQL 模式。該領(lǐng)域的結(jié)構(gòu)是動態(tài)的。我只能讀取和渲染它作為字符串(渲染結(jié)構(gòu)中的字符串類型):[ { "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:該字段的結(jié)構(gòu)是動態(tài)的。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:結(jié)構(gòu)實際結(jié)構(gòu)是:type BannerSerializer struct { ID int Name string DisplayInfo string Format formatSerializer Weight float32 Application int Url string Referrer string} DisplayInfo 序列化為 base64 字符串(或者像 base64,不知道)
- 3 回答
- 0 關(guān)注
- 215 瀏覽
添加回答
舉報
0/150
提交
取消