dtz55359 2015-08-05 04:41
浏览 33
已采纳

去“ encoding / json”:元数据json字段

I have a PostgreSQL schema with json field's (DisplayInfo, and FormatInfo). Structure of this field's is dynamic.

I'can read and render it only as string (string type in render struct) :

[
 {  
  "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":""
 }
]

but i want output field DisplayInfo as JSON object. How ?

My render code:

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 : Structure of this field's is dynamic. DisplayInfo may have 'path' field, or may not. They may have additional fields.

UPD 2. I wana output DisplayInfo and FormatInfo as json-object(not string), as part of whole object, like this:

[
 {  
  "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: Structures

Actual structure is :

type BannerSerializer struct {
    ID          int
    Name        string
    DisplayInfo string
    Format      formatSerializer
    Weight      float32
    Application int
    Url         string
    Referrer    string
}  

Then i trying this structure:

type BannerSerializer struct {
    ID          int
    Name        string
    DisplayInfo json.RawMessage
    Format      formatSerializer
    Weight      float32
    Application int
    Url         string
    Referrer    string
}   
  • DisplayInfo serialize as base64 string (or like base64, don't know)
  • 写回答

3条回答 默认 最新

  • dpkk8687 2015-08-05 06:48
    关注

    Assuming you have access to change models.Model, you can create your own type with a custom Unmarshaler that just returns the raw string:

    type JSONString string
    
    func (s JSONString) MarshalJSON() ([]byte, error) {
        return []byte(s), nil
    }
    

    Working example:

    package main
    
    import (
        "encoding/json"
        "fmt"
    )
    
    type JSONString string
    
    func (s JSONString) MarshalJSON() ([]byte, error) {
        return []byte(s), nil
    }
    
    type Model struct {
        ID          int
        Name        string
        DisplayInfo JSONString
    }
    
    func main() {
        data := []byte(`{   
      "ID":9,
      "Name":"120 №1",
      "DisplayInfo":"{\"path\": \"http://path/to/img.png\"}"
    }`)
    
        var obj Model
        err := json.Unmarshal(data, &obj)
        if err != nil {
            panic(err)
        }
    
        // Here comes your code
        js, err := json.Marshal(obj)
    
        if err != nil {
            panic(err)
        }
    
        fmt.Println(string(js))
    }
    

    Output:

    {"ID":9,"Name":"120 №1","DisplayInfo":{"path":"http://path/to/img.png"}}

    Playground: http://play.golang.org/p/6bcnuGjlU8

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端