doujieyu7062 2019-03-20 10:28 采纳率: 0%
浏览 43
已采纳

Go JSON元帅的默认案例选项?

I have the following structures to export onto json:

type ExportedIncident struct {
    Title      string `json:"title"`
    Host       string `json:"host"`
    Status     string `json:"status"`
    Date       string `json:"date"`
    Notes      []ExportedNote `json:"notes"`
    LogEntries []ExportedLogEntry `json:"log_entries"`
}

And I want underscore cased fields, so I had to define each field just for that as described in this answer: https://stackoverflow.com/a/11694255/1731473

But it's really cumbersome and I believe there is a simpler solution in Go, but I can't find it.

How can I set default letter-case (underscore, snake, camel...) for JSON export?

  • 写回答

2条回答 默认 最新

  • dsfg3241 2019-03-20 11:00
    关注

    Unfortunately there are no opportunities to export your fields into snake_case so you have to maintain tags by yourself.

    Technically you can use method MarshalJSON and perform all manipulations inside this method, but it isn't easier way...

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

报告相同问题?

悬赏问题

  • ¥15 想用@vueuse 把项目动态改成深色主题,localStorge里面的vueuse-color-scheme一开始就给我改成了dark,不知道什么原因(相关搜索:背景颜色)
  • ¥20 OPENVPN连接问题
  • ¥15 flask实现搜索框访问数据库
  • ¥15 mrk3399刷完安卓11后投屏调试只能显示一个设备
  • ¥20 白日门传奇少一个启动区服和启动服务器的快捷键,东西都是全的 , 他们说套一个出来就行了 但我就是弄不好,谁看看,
  • ¥100 如何用js写一个游戏云存档
  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题