dongpo1216 2019-04-12 04:24
浏览 327
已采纳

如何制作[]数组并使用包含[]数组进行映射

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.

  • 写回答

2条回答 默认 最新

  • dongmei8460 2019-04-12 06:44
    关注

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧