我正在使用 Bing Search API 的 json 结果。结果,双引号被一个反斜杠转义。然而,Javascript 不接受这一点。它要求我使用双反斜杠转义双引号。所以,我的问题是如何用双反斜杠替换单反斜杠。比如一部分的json代码是这样的
"Description":"LONDON Britain should stay in the EU \"warts and all\", the opposition Labour leader will say on Thursday..."
我希望它是这样的
"Description":"LONDON Britain should stay in the EU \\"warts and all\\", the opposition Labour leader will say on Thursday..."
我尝试了以下解决方案
json = '"Description":"LONDON Britain should stay in the EU \"warts and all\", the opposition Labour leader will say on Thursday..."';
dfe = JSON.stringify(json);
dfe = dfe.replace(/\\"/g,'\\\\"');
但是,它没有用。它替换了所有双引号之前的所有反斜杠。从此...
\"Description\":\"LONDON Britain should stay in the EU \"warts and all\", the opposition Labour leader will say on Thursday...\"
……到这个
\\"Description\\":\\"LONDON Britain should stay in the EU \\"warts and all\\", the opposition Labour leader will say on Thursday...\\"
谁能告诉我如何用 \\" 替换 \" ?
编辑:我想做的是这个
<p id="demo"></p>
var json = '{"d":{"results":[{"__metadata":{"uri":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/News?Query=\u0027britain\u0027&$skip=1&$top=1","type":"NewsResult"},"ID":"f1c27ae7-bf16-4741-a789-897f4878c2e1","Title":"Britain should stay in EU \u0027warts and all\u0027 - Corbyn | Reuters","Url":"http://www.firstpost.com/world/britain-should-stay-in-eu-warts-and-all-corbyn-reuters-2728514.html","Source":"Firstpost","Description":"LONDON Britain should stay in the EU \"warts and all\", the opposition Labour leader will say on Thursday, making his first big intervention in the referendum campaign as he seeks to counter criticism he is not doing enough to persuade his voters to back the ...","Date":"2016-04-14T05:10:45Z"}],"__next":"https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/News?Query=\u0027britain\u0027&$skip=10&$top=10"}}';
obj = JSON.parse(json);
document.getElementById("demo").innerHTML = obj.d.results[0].Title;