jsonapigocontrollerbeego

How to make a []array & map with contains []array


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
    }
}

please help me with this error

listUsers []users.User users map[int]interface{}

I just gotta add more details, when I get the answer.


Solution

  • 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)