如何美化 json 中的输出?
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)
}
使用json.Indent
:
resp, _ := json.Marshal(...)
dst := bytes.Buffer{}
json.Indent(&dst,resp,""," ")
w.Write(dsr.Bytes())
改变
resp, _ := json.Marshal(map[string]string{...})
到
resp, _ := json.MarshalIndent(map[string]string{...}, "", " ")
所以我写了这个非常简单的 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)
}
使用json.Indent
:
resp, _ := json.Marshal(...)
dst := bytes.Buffer{}
json.Indent(&dst,resp,""," ")
w.Write(dsr.Bytes())
改变
resp, _ := json.Marshal(map[string]string{...})
到
resp, _ := json.MarshalIndent(map[string]string{...}, "", " ")