[英]How do i beautify the output in json?
所以我寫了這個非常簡單的 go 應用程序,它在 JSON 中顯示了一堆信息,但所有輸出數據都被縮小了,我需要一些幫助來美化所有數據。
func ExampleHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "application/json")
resp, _ := json.Marshal(map[string]string{
"accept": r.Header.Get("Accept"),
})
w.Write(resp)
}
改變
resp, _ := json.Marshal(map[string]string{...})
到
resp, _ := json.MarshalIndent(map[string]string{...}, "", " ")
使用json.Indent
:
resp, _ := json.Marshal(...)
dst := bytes.Buffer{}
json.Indent(&dst,resp,""," ")
w.Write(dsr.Bytes())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.