1 回答

TA貢獻1793條經驗 獲得超6個贊
對于 Marshaling 和 Unmarshaling,您應該將 struct 字段定義為導出字段,并且目標應該是 regs 的映射。此外 bool 類型對 the 無效,Enabled您應該將其更改為 int
type regs struct {
Enabled int `json:"enabled,omitempty"`
Pct float64 `json:"pct,omitempty"`
}
func main() {
a := `{
"1": {
"enabled": 1,
"pct": 0.5
},
"2": {
"enabled": 1,
"pct": 0.6
},
"3": {
"enabled": 1,
"pct": 0.2
},
"4": {
"enabled": 1,
"pct": 0.1
}
}`
dest := make(map[string]regs)
json.Unmarshal([]byte(a), &dest)
fmt.Println(dest)
}
輸出將是:
map[1:{1 0.5} 2:{1 0.6} 3:{1 0.2} 4:{1 0.1}]
- 1 回答
- 0 關注
- 117 瀏覽
添加回答
舉報