duanhunlou7051 2014-06-20 10:29
浏览 38
已采纳

获取在Go json解组中出现错误的字段名称

I have some big json files that are slightly different in the types that the fields contain.

{ "a":"1" }

vs.

{ "a":1 }

When I unmarshal the second I get:

cannot unmarshal number into Go value of type string

However since these jsons are large I would like to have the actual field that is in error so I can fix them. The UnmarshalTypeError does not hold the Struct's field type.

Does anybody know of a way to get to field name? (not debugging I have a lot of different fields that err)

[EDIT] I know how to solve the type conversion. What I need is a method to see what fields I need to apply that conversion to.

  • 写回答

1条回答 默认 最新

  • doula4096 2014-06-20 12:13
    关注

    The short answer is that you can't.

    However, to fix your problem, there is multiple solutions:

    • Dive into the json.Unmarshal source code to change its working and add the information you need: copy the function to a local package, do your edits, and use this function
    • Use a thrid-party tool to help you, for example a JSON validator compatible with JSON Schema: here is an online example, there is probably some better-suited tool
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?