How can I pretty-print JSON using Go?

后端 未结 11 1621
挽巷
挽巷 2020-12-07 08:12

Does anyone know of a simple way to pretty-print JSON output in Go?

The stock http://golang.org/pkg/encoding/json/ package does not seem to include functiona

11条回答
  •  醉酒成梦
    2020-12-07 08:27

    By pretty-print, I assume you mean indented, like so

    {
        "data": 1234
    }
    

    rather than

    {"data":1234}
    

    The easiest way to do this is with MarshalIndent, which will let you specify how you would like it indented via the indent argument. Thus, json.MarshalIndent(data, "", " ") will pretty-print using four spaces for indentation.

提交回复
热议问题