去“ 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)

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

You'd have to unmarshal it, here's an example:

var data []*struct {
    ID int
    DisplayInfo string
}
if err := json.Unmarshal([]byte(j), &data); err != nil {
    log.Fatal(err)
}
for _, d := range data {
    var displayInfo struct{ Path string }
    if err := json.Unmarshal([]byte(d.DisplayInfo), &displayInfo); err != nil {
        log.Fatal(err)
    }
    fmt.Println(d.ID, displayInfo.Path)
}

playground

Use a pointer to json.RawMessage:

type Data struct {
    Obj *json.RawMessage
}

Playground: http://play.golang.org/p/Qq9IUBDLzJ.