使用正则表达式的反斜杠多余的双引号

时间:2020-04-07 14:53:29

标签: javascript node.js json regex

我要解析的字符串中包含错误的JSON。使它不正确的原因是其中包含双引号字符。


不正确的JSON

{ "key": "the log that is broken use the character " which brake the json", "key2": "value2" }


正确的JSON

{ "key": "the log that is broken use the character \" which brake the json", "key2": "value2" }


replace是否可以使用任何正则表达式来抵消多余的",以便我可以解析JSON?

const json = `{ "key": "the log that is broken use the character " which brake the json", "key2": "value2" }`;

// Do the regex and the replace ...

const obj = JSON.parse(json);

console.log(obj);

我已经尝试了多个正则表达式,但是无法正常工作。另外,如果您只是领先者,我会亲自尝试一下。

谢谢



编辑:

这是我编写的有效解决方案。如果您问我很多,我将不胜感激:

const json = `{ "key": "the log that is broken use the character " which brake the json", "key2": "value2" }`;

console.log(json);

// Get all occurences of the troubled strings
const [
  _,
  ...results
] = /(?:\: "(?:(.*)*)",)|(?:\: "(?:(.*)*)" )/.exec(json);

let mutatedJson = json;

// For each occurence, replace the extra double quotes, then apply it on the main string
results.slice(0, results.length - 1).forEach((x) => {
   const oldVal = x;
   
   // Take the string without the first and last double quotes and backslash remaining quotes
   const newVal = x.replace('"', "\\\"");
   
   // Insert the clean data in
   mutatedJson = mutatedJson.replace(oldVal, newVal);
});

const obj = JSON.parse(mutatedJson);

console.log(obj);

1 个答案:

答案 0 :(得分:1)

这是您可以做什么的一个例子。

var json = `{ "key": "the log that is broken use " the character " which brake the json", "key2": "value2" }`;

var problemKey = "key";

var problemStart = json.indexOf(problemKey) + problemKey.length + 1;
var valueStart = json.indexOf("\"", problemStart) + 1;
var valueEnd = json.indexOf(",", problemStart);
var value = json.substring(valueStart, valueEnd);
valueEnd = json.lastIndexOf("\"", valueEnd);
console.log(json.substring(valueStart, valueEnd));
value = json.substring(valueStart, valueEnd);

json = json.substring(0, valueStart)
	+ json.substring(valueStart, valueEnd).replace(/"/g, "\\\"")
	+ json.substring(valueEnd);
  
console.log(json);

const obj = JSON.parse(json);

console.log(obj);

这里的(强)假设是两个

  1. 您知道有问题的密钥,
  2. 这些值不包含逗号。

如果您的值可以包含逗号,那么您可以通过查找下一个键来再次找到问题值的结尾,再次假设您确定这是什么。

正如其他人在评论中所说的,当然,更好的方法肯定是在源头修复JSON。