How to make a map like "Data":[{"3":{...}},{"4":{...}}]
Controller
jsons := make(map[string]interface{})
listUsers := identity.ListUsers()
users := make(map[int]interface{})
for k, j := range listUsers {
if j.Description != "" {
users[k] = j
}
}
jsons["Data"] = users
u.Data["json"] = jsons
u.ServeJSON()
JSON
"Data": {
"3": {
"default_project_id": "",
"description": "description",
"domain_id": "default",
"enabled": true
},
"5": {
"default_project_id": "9e266e1a750e45f8862e83341a5d0970",
"description": "description",
"domain_id": "default",
"enabled": true
}
}
listUsers []users.User users map[int]interface{}
I just gotta add more details, when I get the answer.
Firstly,"Data":[ "3":{...},"4":{...} ]
is not a valid format of json.You can't put key-value data inside []
except {}
.So something inside []
must be list.So you can change it like "Data":[{"3":{...}},{"4":{...}}]
.
Then change the controller code like users := make([]map[int]interface{},0)
I think you should create new types:
type AutoGenerated struct {
DefaultProjectID string `json:"default_project_id"`
Description string `json:"description"`
DomainID string `json:"domain_id"`
Enabled bool `json:"enabled"`
}
type MyType map[int]AutoGenerated
I think that is not a good idea to create a slice of map but can:
type SliceMap []MyType
To generate new go-types from JSON you can use: https://mholt.github.io/json-to-go/