考慮以下🎜>
{ "1001":{ "level":10, "monster-id":1001, "skill-level":1, "aimer-id":301 }, "1002":{ "level":12, "monster-id":1002, "skill-level":1, "aimer-id":302 }, "1003":{ "level":16, "monster-id":1003, "skill-level":2, "aimer-id":303 } }目標是將這個JSON 解組到Monster陣列中
type Monster struct { MonsterId int32 Level int32 SkillLevel int32 AimerId int32 }解決方案要實現此目的,請按照下列步驟操作:
type MonsterCollection struct { Pool map[string]Monster }
func (mc *MonsterCollection) FromJson(jsonStr string) error { var data *map[string]Monster b := []byte(jsonStr) return json.Unmarshal(b, &data) }
type Monster struct { MonsterId int32 `json:"monster-id"` Level int32 `json:"level"` SkillLevel int32 `json:"skill-level"` AimerId int32 `json:"aimer-id"` }
mc := new(MonsterCollection) err := mc.FromJson(jsonStr)
for id, monster := range mc.Pool { fmt.Println(id, monster) }按照以下步驟,您可以成功地將巢狀的JSON 資料解組到Go 中的Monster 物件數組中。
以上是如何在 Go 中將巢狀 JSON 解組到物件數組中?的詳細內容。更多資訊請關注PHP中文網其他相關文章!