我需要从 Json 数组创建一个地图,我从下面的 GO 开始,有点卡住,有什么指针吗? package mainimport ( "encoding/json" "fmt" "strconv")func main() { jsonStr := `{ "employee": [ { "id": 14325, "grpname": "senior" }, { "id": 234, "grpname": "junior" } ]}` type Group struct { Employee []struct { GroupName string GroupId int } } var group []Group var groupMap []map[string]interface{} err := json.Unmarshal([]byte(jsonStr), &groupMap) if err != nil { panic(err) } for _, groupData := range groupMap { // convert map to array of Group struct var g Group g.GroupName = fmt.Sprintf("%s", groupData["grpname"]) g.GroupId, _ = strconv.Atoi(fmt.Sprintf("%v", groupData["id"])) group = append(group, g) } fmt.Println(group)}错误:./prog.go:45:4: g.GroupName undefined (type Group has no field or method GroupName)./prog.go:46:4: g.GroupId undefined (type Group has no field or method GroupId)预期输出:{"senior": 14325,"junior": 234}尝试了一些类似下面的操作,但出现错误:出现错误:%!(EXTRA string=json: cannot unmarshal array into Go struct field GetEmpResponse.employee of type map[string][]model.Employee)类型 GetEmpResponse 结构 { Employee map[string][]Employee json:"employee" }为我的测试尝试简化Json,请参考 play.golang.org
- 1 回答
- 0 关注
- 98 浏览
添加回答
举报
0/150
提交
取消