1 回答

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊
文件格式的 JSON 數(shù)組是雙重編碼的。聲明一個(gè)與數(shù)組對(duì)應(yīng)的 Go 類型。在該類型的 UnmarshalJSON 方法中雙重解碼。
type FileFormats []FileFormat
func (ff *FileFormats) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
return json.Unmarshal(
[]byte(s),
(*[]FileFormat)(ff))
}
type TokenMetadata struct {
Name string `json:"name"`
Formats FileFormats `json:"formats"`
}
注意:為了防止遞歸,需要從 from*FileFormats到 to的轉(zhuǎn)換。*[]FileFormat
- 1 回答
- 0 關(guān)注
- 196 瀏覽
添加回答
舉報(bào)