1 回答

TA貢獻1776條經驗 獲得超12個贊
您需要某個地方來存儲 json 字符串accounts。用一個:
var m map[string][]AccountProperties
就足夠了,盡管您當然需要知道使用字符串文字accounts來訪問由此創建的(單個)映射條目:
type AccountProperties struct {
? ? ID? ? ? ? ? ?string? ?`json:"id"`
? ? MT4AccountID int? ? ? `json:"mt4AccountID,omitempty"`
? ? Tags? ? ? ? ?[]string `json:"tags"`
}
func main() {
? ? var m map[string][]AccountProperties
? ? err := json.Unmarshal([]byte(data), &m)
? ? fmt.Println(err, m["accounts"])
}
D
(我必須更改to的類型string
并修復{
json 中缺失的內容)。
這并不比僅使用匿名結構類型短:
var?a?struct{?Accounts?[]AccountProperties?}
就調用而言Unmarshall
(這樣完成后使用起來更方便)。如果您想在調用中使用這樣的匿名結構json.Marshall
,則需要標記其單個元素以獲得小寫編碼:如果沒有標記,它將被調用"Accounts"
而不是"accounts"
.
(我并不認為地圖方法更好,只是一種替代方法。)
- 1 回答
- 0 關注
- 119 瀏覽
添加回答
舉報