json - 如何使用包含 []array 制作 []array & map
问题描述
如何制作类似的地图"Data":[{"3":{...}},{"4":{...}}]
控制器
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 用户映射[int]interface{}
当我得到答案时,我只需要添加更多细节。
解决方案
我认为您应该创建新类型:
type AutoGenerated struct {
DefaultProjectID string `json:"default_project_id"`
Description string `json:"description"`
DomainID string `json:"domain_id"`
Enabled bool `json:"enabled"`
}
类型 MyType map[int]AutoGenerated
我认为创建一片地图不是一个好主意,但可以:
type SliceMap []MyType
要从 JSON 生成新的 go-types,您可以使用:https ://mholt.github.io/json-to-go/
推荐阅读
- angular - 将变量分配为数组中的键
- php - 使用 php 查找数组的第一个元素并将其添加到活动类以使用 bootstrap.js 创建选项卡系统
- ruby - 从数组中创建散列,其中值是元素的索引
- android - Kotlin Activity 将书写顺序从左到右更改为从右到左(带有不同语言的电话)
- scala - 与输入字符串匹配的字符串列表
- pom.xml - 我在这个 POM 中的错误是什么以及在哪里?
- python - Pygame 无法加载 PNG 图像
- python - Python 的 time.time 函数在全球范围内的准确性如何?
- jquery - 下拉菜单立即打开
- java - 错误:在加入 2 个流或在 Kafka 中创建 KTable 时出现 librocksdbjni6770528225908825804.dll