在 Go 中使用 JSON Marshal 的小写 JSON 键名

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?

转载于:https://stackoverflow.com/questions/11693865/lowercase-json-key-names-with-json-marshal-in-go

csdnceshi79
python小菜 I misread your question as about struct key names, not JSON key names. Retracted the vote.
3 年多之前 回复
csdnceshi68
local-host Don't apologize. You're not the silly one, the language that does not accept the full json spec is the silly one (although the issue is easily solved, as below).
接近 5 年之前 回复
csdnceshi67
bug^君 I would say: a key in a client/server communication protocol strictly defined to lower case letters. Well, in my case it doesn't matter since I have defined the protocol myself.. but in theory at least? But, I know it is mainly just me spending too much time on a silly little detail.
大约 8 年之前 回复

2个回答

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"
}
weixin_41568127
?yb? To be frank I was searching for this a lot...thanks
3 年多之前 回复
csdnceshi80
胖鸭 This could be made even better with some inspiration from Gson, which allows setting any "field naming policy" like LOWER_CASE_WITH_UNDERSCORES (aka snake case).
3 年多之前 回复
csdnceshi78
程序go Make sure there is no space between the colon and the first quotation mark of the tag! Use json:"some_tag" instead of json: "some_tag". I got bit by this for a while.
大约 4 年之前 回复
csdnceshi70
笑故挽风 I've just started to curse the go language - how stupid is this, why would they make the fields with lowercase letters in the generated JSON etc, etc. Then I came across this thread and thought "OMG That's brilliant!!!". I even jumped and explained my girlfriend why I am so excited :D It's so cool :)))
6 年多之前 回复
weixin_41568196
撒拉嘿哟木头 Yeah, when working with reflection I see how having a way to add meta data to a field can be a wonderful thing! Btw, just tried the answer. Works like a charm.
大约 8 年之前 回复
csdnceshi77
狐狸.fox They are quite handy. You can access them at runtime through the reflect package.
大约 8 年之前 回复
weixin_41568196
撒拉嘿哟木头 Field tags? Oh my.. I missed this entire part when searching in the docs. I was looking for flags, functions or some other settings. Well, this is the exact answer I was looking for! And in addition, I have a new Go concept to learn about: field tags :)
大约 8 年之前 回复

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.

weixin_41568110
七度&光 Yeah, I wasn't clear whether or not it was an external package or not. But your answer is still relevant and useful in the cases when you don't control the definitions.
大约 8 年之前 回复
csdnceshi79
python小菜 Oh, my answer was predicated on you not being in control of the definition of the struct. jimt's answer is definitely what you want.
大约 8 年之前 回复
weixin_41568110
七度&光 The silly thing is that the other package (containing the types) is actually mine as well. But, yes, I have probably stared to blindly at the fact that it must be a struct. Using a map[string]interface{} would work as long as I don't end up with nested objects/structs
大约 8 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐