package main import ( "bytes" "encoding/json" "fmt" ) func main() { // 原始 JSON 字符串 var jsonStr = []byte(`{"Name":"Gopher","Title":"Programmer","Contact":{"Email":"[email protected]","Phone":"1234567890","Address":"123 Main St."},"Skills":["Go","Java","C"]}`) // 定义结构体 type Person struct { Name string Title string Contact struct { Email string Phone string Address string } Skills []string } // 将 JSON 数据解析为结构体 //var person Person //if err := json.Unmarshal(jsonStr, &person); err != nil { // panic(err) //} // 格式化 JSON 数据并输出 var output bytes.Buffer if err := json.Indent(&output, jsonStr, "", "\t"); err != nil { panic(err) } fmt.Println(output.String()) }
标签:jsonStr,格式化,string,err,JSON,var,json,go From: https://www.cnblogs.com/zxqblogrecord/p/17776259.html