区分大小写的 JSON Unmarshal 结构

标签 json go unmarshalling

有没有办法让 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/

相关文章:

javascript - 如何在单选按钮单击时获取 json 存储的数据

python - 在 Pandas 中解析 JSON

c# - 嵌套结构解码

java - 将 XML 文件转换为 Java 对象时出现 "Class has two properties of the same name"错误

php - 如何高效地通过JSON从MYSQL服务器获取BLOB图像文件

java - 更有效地解析 json?

去插件调试

go - Delve 是否支持更改或某些变体的热交换?

http - 去 Gin 框架 : Testing query and POST with cURL

java - Java/Kotlin 中 XML 解码为对象