1 回答

TA貢獻(xiàn)1825條經(jīng)驗(yàn) 獲得超4個(gè)贊
我會(huì)使用一些流行的 toml 庫將 toml 解析為結(jié)構(gòu),然后使用標(biāo)準(zhǔn)庫將其編組為 JSON。
下面的代碼使用了https://github.com/BurntSushi/toml。
請注意,為簡潔起見,沒有錯(cuò)誤處理。
type Config struct {
Age int `json:"age,omitempty"`
Cats []string `json:"cats,omitempty"`
Pi float64 `json:"pi,omitempty"`
Perfection []int `json:"perfection,omitempty"`
DOB time.Time `json:"dob,omitempty"`
}
var tomlData = `
Age = 25
Cats = [ "Cauchy", "Plato" ]
Pi = 3.14
Perfection = [ 6, 28, 496, 8128 ]
DOB = 1987-07-05T05:45:00Z`
func main() {
var conf Config
toml.Decode(tomlData, &conf)
j, _ := json.MarshalIndent(conf, "", " ")
fmt.Println(string(j))
}
輸出:
{
"age": 25,
"cats": [
"Cauchy",
"Plato"
],
"pi": 3.14,
"perfection": [
6,
28,
496,
8128
],
"dob": "1987-07-05T05:45:00Z"
}
https://play.golang.com/p/91JqFjkJIXI
- 1 回答
- 0 關(guān)注
- 356 瀏覽
添加回答
舉報(bào)