Files
ai/models/ListView.go
2023-05-12 14:16:27 +08:00

39 lines
813 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package models
import (
"encoding/json"
"log"
"net/http"
)
type ListView struct {
Page int `json:"page"`
PageSize int `json:"page_size"`
Total int `json:"total"`
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
}
// 直接輸出JSON給瀏覽器
func (listview *ListView) WriteJSON(w http.ResponseWriter) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(listview.ToJSON())
}