JSON中的键上缺少引号 [英] missing quotation marks on keys in JSON

查看:308
本文介绍了JSON中的键上缺少引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含格式错误的JSON的字符串,该字符串在缺少键的情况下提供给我. JSON的结构超出了我的控制范围,因此我需要使用已有的东西. 我发现在JavaScript中解析格式错误的JSON 的OP可以解决该问题的解决方案,但是其中一个值包含一个URL RegEx匹配并将其转换为另一个键(如value),从而导致JSON真正损坏.有什么想法吗?

I have a string containing malformed JSON which is being provided to me where the keys are missing the quotation marks. The structure of the JSON is out of my control, so I need to work with what I have. I have found the solution that the OP posts in Parsing malformed JSON in JavaScript works, however one of the values contains a URL that the RegEx matches and transforms it into another key like value, resulting in really broken JSON. Any ideas?

我还查看了 json修复,但在此没有太大的成功.

I have also looked at jsonrepair, but not having much success there.

推荐答案

这应该可以做到.您需要做的就是确定冒号后面是正斜杠(例如http://)的方式,而不是孤立的方式.请注意,如果您的JSON值之一包含冒号,则此操作将失败,因此可能需要针对您的用例进行更多改进.

This should do it. All you needed to do was identify when a colon was followed by a forward-slash (like in http://) instead of in isolation. Note that this will fail in the event that one of your JSON values has a colon in it, so it may need more improvement for your use case.

.replace(/(['"])?([a-zA-Z0-9_]+)(['"])?:([^\/])/g, '"$2":$4');

这篇关于JSON中的键上缺少引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆