我正在尝试修复其他人的 JSON 字符串,以便我可以将其解析为真正的 JSON 对象。 JSON 在数组的最后一个对象后面有逗号,因此它无效。我正在尝试删除它,但我无法获得正确的公式。有什么想法吗?
示例字符串
"attributes":[]},]},
应该成为
"attributes":[]}]},
我尝试了以下方法,但都没有成功。
string = string.replaceAll('},],', '}],');
string = string.replaceAll('\},\],', '\}\],');
string = string.replaceAll('\}\,\],', '\}\]\,');
你可以评估它(虽然它会慢
JSON.parse
,但可能不会比替换时慢):
const parsed = eval('{items:[{"attributes":[]},]}');
console.log(parsed);