JSON Unmarshal struct case-sensitively

别说谁变了你拦得住时间么 提交于 2019-12-04 02:57:09

问题


Is there any way to make json.Unmarshal not accept a case-insensitive match? I receive a JSON with tags such as "e" and "E" and would like to unmarshal the object with tag "e" but ignore the one with "E". Right now the only solution I found was to define a struct containing both tags and then to simply ignore tag "E", but I'm looking for a cleaner solution.

From the official doc:

To unmarshal JSON into a struct, Unmarshal matches incoming object keys to the keys used by Marshal (either the struct field name or its tag), preferring an exact match but also accepting a case-insensitive match.


回答1:


Unfortunately this is not something currently supported by the standard json library.

According to https://golang.org/pkg/encoding/json/#Unmarshal

Unmarshal matches incoming object keys to the keys used by Marshal (either the struct field name or its tag), preferring an exact match but also accepting a case-insensitive match

There is no way to turn this behaviour off.



来源:https://stackoverflow.com/questions/49006073/json-unmarshal-struct-case-sensitively

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!