亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

控制器將 json 作為字符串返回

控制器將 json 作為字符串返回

Go
慕哥9229398 2022-07-11 15:24:44
我從數據庫中加載了一個項目列表,并希望將其作為 Json 返回。它運行良好,但響應不是 json 對象,它的字符串是接收方的問題。var products []*database.Productnum, err := o.QueryTable("product").Filter("date", date).All(&products)fmt.Printf("Returned Rows Num: %d, %s", num, err)var jsonData []bytejsonData, err2 := json.Marshal(products)if err2 != nil {    fmt.Println(err)}this.Data["json"] = string(jsonData)this.ServeJSON()它返回"[{\"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...添加注釋 likejson:"id"不會改變任何東西。這是一個問題,因為它是一個數組?我必須將其包裝在結構中嗎?
查看完整描述

1 回答

?
忽然笑

TA貢獻1806條經驗 獲得超5個贊

正如 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()


查看完整回答
反對 回復 2022-07-11
  • 1 回答
  • 0 關注
  • 148 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號