使用Go中的JSON Marshal将JSON键名转换为小写

huangapple go评论110阅读模式
英文:

Lowercase JSON key names with JSON Marshal in Go

问题

我希望使用"encoding/json"包来编组在我的应用程序的导入包中声明的结构体。

例如:

type T struct {
    Foo int
}

因为它是导入的,结构体中所有可用(公开的)字段都以大写字母开头。但是我希望有小写的键名:

out, err := json.Marshal(&T{Foo: 42})

将会得到

{"Foo":42}

但是我希望得到

{"foo":42}

有没有办法简单地解决这个问题?

英文:

I wish to use the "encoding/json" package to marshal a struct declared in one of the imported packages of my application.

Eg.:

type T struct {
    Foo int
}

Because it is imported, all available (exported) fields in the struct begins with an upper case letter. But I wish to have lower case key names:

out, err := json.Marshal(&T{Foo: 42})

will result in

> {"Foo":42}

but I wish to get

> {"foo":42}

Is it possible to get around the problem in some easy way?

答案1

得分: 333

请查看encoding/json.Marshal的文档。它讨论了使用结构体字段标签来确定生成的JSON的格式。

例如:

type T struct {
    FieldA int    `json:"field_a"`
    FieldB string `json:"field_b,omitempty"`
}

这将生成以下JSON:

{
    "field_a": 1234,
    "field_b": "foobar"
}
英文:

Have a look at the docs for encoding/json.Marshal.
It discusses using struct field tags to determine how the generated json is formatted.

For example:

type T struct {
    FieldA int    `json:"field_a"`
    FieldB string `json:"field_b,omitempty"`
}

This will generate JSON as follows:

{
    "field_a": 1234,
    "field_b": "foobar"
}

答案2

得分: 10

你可以创建自己的结构体,其中包含你想要导出的键,并为它们提供适当的 JSON 标签以使用小写名称。然后,在将其编码为 JSON 之前,你可以将所需的结构体复制到你的结构体中。或者,如果你不想麻烦地创建一个本地结构体,你可以使用 map[string]interface{} 并对其进行编码。

英文:

You could make your own struct with the keys that you want to export, and give them the appropriate json tags for lowercase names. Then you can copy the desired struct into yours before encoding it as JSON. Or if you don't want to bother with making a local struct you could probably make a map[string]interface{} and encode that.

答案3

得分: 5

我只想补充一点,你可以使用gopls自动生成这些标签。手动添加标签是一项繁琐的任务,特别是对于大型的JSON结构,所以这个功能非常方便。

根据个人偏好的编辑器,添加gopls langserver的方法有所不同。在执行以下命令之后:

go install golang.org/x/tools/gopls@latest

对于使用CoC的Neovim用户,可以使用:CocInstall coc-go命令,然后执行go.tags.add。有关CoC扩展的完整文档,请参阅这里

英文:

I will only add that you can generate those tags automatically using gopls. It is a menial task to add the tags manually, especially with large json structs, so the feature is a live-saver.

Adding the gopls langserver differs based on one's preferred editor. After:

go install golang.org/x/tools/gopls@latest

For Neovim with CoC you can :CocInstall coc-go and then go.tags.add. For complete docs on the CoC extension for go please see here.

答案4

得分: 0

你可以使用fatih/gomodifytags生成结构字段的json:"camelCase"标签。

例如:

$ gomodifytags -file main.go -struct T -add-tags json -transform camelcase -quiet -w

注意:你也可以使用-override来覆盖现有的标签。

英文:

You can generate the json:"camelCase" tags of struct fields with fatih/gomodifytags.

e.g.

$ gomodifytags -file main.go -struct T -add-tags json -transform camelcase -quiet -w

NB: You can also use -override to override existing tags.

huangapple
  • 本文由 发表于 2012年7月28日 02:46:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/11693865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定