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 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件