GO語言"encoding/json"包中"MarshalIndent"函數的用法及代碼示例。
用法:
func MarshalIndent(v any, prefix, indent string)([]byte, error)
MarshalIndent 類似於 Marshal,但應用縮進來格式化輸出。根據縮進嵌套,輸出中的每個 JSON 元素都將在以前綴開頭的新行開始,後跟一個或多個縮進副本。
例子:
package main
import (
"encoding/json"
"fmt"
"log"
)
func main() {
data := map[string]int{
"a": 1,
"b": 2,
}
b, err := json.MarshalIndent(data, "<prefix>", "<indent>")
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}
輸出:
{ <prefix><indent>"a": 1, <prefix><indent>"b": 2 <prefix>}
相關用法
- GO Marshal用法及代碼示例
- GO MakeFunc用法及代碼示例
- GO Map用法及代碼示例
- GO MakeTable用法及代碼示例
- GO MatchString用法及代碼示例
- GO Match用法及代碼示例
- GO MethodSet用法及代碼示例
- GO Mul32用法及代碼示例
- GO Mkdir用法及代碼示例
- GO MkdirTemp用法及代碼示例
- GO Modf用法及代碼示例
- GO Mul64用法及代碼示例
- GO Mod用法及代碼示例
- GO MultiReader用法及代碼示例
- GO MultiWriter用法及代碼示例
- GO Month用法及代碼示例
- GO MkdirAll用法及代碼示例
- GO PutUvarint用法及代碼示例
- GO Scanner.Scan用法及代碼示例
- GO LeadingZeros32用法及代碼示例
注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 MarshalIndent。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。