doushun9875 2018-02-27 10:09
浏览 510
已采纳

区分大小写的JSON Unmarshal结构

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条回答 默认 最新

  • doushan7997 2018-02-27 10:36
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献