dongweihuan8610 2019-05-24 07:36
浏览 65
已采纳

如何在一个字段中创建nil值的地图,然后将其封送给json?

I need get json string

{"Error": null }

I can do this using this approach

type OKResponse struct {
    Error *int `json:"Error"`
}

encoded, err := json.Marshal(OKResponse{})
...

How I can get the same result using map? Or it is not possible?

For example something like this


jsonbody := map[string]int{"Error": nil}
encoded, err := json.Marshal(&jsonbody)
...
  • 写回答

3条回答 默认 最新

  • dongwoqin7034 2019-05-24 07:54
    关注

    For this particular case you may use:

    jsonbody := map[string]error{"Error": nil}
    

    But for more generalized you may also use:

    jsonbody := map[string]interface{}{"Error": nil}
    

    Both approaches will produce:

    {"Error":null}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信