且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JSON中的键上缺少引号

更新时间:2022-02-27 08:46:02

这应该可以做到.您需要做的就是确定冒号后面是正斜杠(例如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');