有没有办法让 json.Unmarshal 不接受不区分大小写的匹配?我收到一个带有“e”和“E”等标签的 JSON,我想解码带有“e”标签的对象,但忽略带有“E”的对象。现在我找到的唯一解决方案是定义一个包含两个标签的结构,然后简单地忽略标签“E”,但我正在寻找一个更简洁的解决方案。
来自官方文档:
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.
最佳答案
不幸的是,这不是标准 json 库当前支持的东西。
根据 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
无法关闭此行为。
关于区分大小写的 JSON Unmarshal 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49006073/