控制器 returns json 作为字符串

Controller returns json as a string

我从数据库中加载了一个项目列表,并希望将其 return 设为 Json。 它运行良好,但响应不是 json 对象,它的字符串是接收方的问题。

var products []*database.Product
num, err := o.QueryTable("product").Filter("date", date).All(&products)
fmt.Printf("Returned Rows Num: %d, %s", num, err)

var jsonData []byte
jsonData, err2 := json.Marshal(products)
if err2 != nil {
    fmt.Println(err)
}

this.Data["json"] = string(jsonData)
this.ServeJSON()

它returns

"[{\"Id\":\"68e7512f-ea50-45d3-a89e-845c7621b33d\",\"Producttypeid\":\"62c9ff0a-f599-4ac1-9442-ebae3bc049c1\",\"Producti...

但应该是

[{"Id":"68e7512f-ea50-45d3-a89e-845c7621b33d","Producttypeid":"62c9ff0a-f599-4ac1-9442-ebae3bc049c1","Producti...

添加像 json:"id" 这样的注释不会改变任何东西。 这是一个问题,因为它是一个数组吗? 我必须将它包装在一个结构中吗?

正如 Kosanovic 的评论中提到的,解决方案非常简单,只是不要自己编组。

var products []*database.Product
num, err := o.QueryTable("product").Filter("date", date).All(&products)
fmt.Printf("Returned Rows Num: %d, %s", num, err)

this.Data["json"] = products
this.ServeJSON()