列表標準格式

This commit is contained in:
2023-04-18 15:53:13 +08:00
parent 62f2e55ffa
commit cf199dfc01
8 changed files with 53 additions and 13 deletions

View File

@@ -1,5 +1,10 @@
package models
import (
"encoding/json"
"log"
)
type ListView struct {
Code int `json:"code"`
Page int `json:"page"`
@@ -8,3 +13,20 @@ type ListView struct {
Next bool `json:"next"`
List []interface{} `json:"list"`
}
// 輸出JSON給瀏覽器
func (listview *ListView) ToJson() []byte {
// 即使list爲空也要返回空的JSON數組
if listview.List == nil {
listview.List = make([]interface{}, 0)
}
// 輸出格式化的JSON
b, err := json.MarshalIndent(listview, "", " ")
if err != nil {
log.Println(err)
return nil
}
return b
}