在JavaScript中将单反斜杠转义替换为双反斜杠

在JavaScript中将单反斜杠转义替换为双反斜杠,javascript,json,bing-api,bing-search,Javascript,Json,Bing Api,Bing Search,我使用的是Bing搜索API的json结果。结果,双引号被一个反斜杠转义。然而,Javascript不接受这一点。它要求我使用双反斜杠来转义双引号。 所以,我的问题是如何用双反斜杠替换单反斜杠。 例如,json代码的一部分如下所示 "Description":"LONDON Britain should stay in the EU \"warts and all\", the opposition Labour leader will say on Thursday..." "Descrip

我使用的是Bing搜索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..."
我希望是这样

"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;

var json='{“d”:{“results”:[{“\uuu元数据”:{“uri”:”https://api.datamarket.azure.com/Data.ashx/Bing/Search/v1/News?Query=\U0027AE7-bf16-4741-a789-897f4878c2e1,“标题”:“英国应该留在欧盟”\u0027warts和all\u0027-科尔宾路透社”;“Url”:"http://www.firstpost.com/world/britain-should-stay-in-eu-warts-and-all-corbyn-reuters-2728514.html“,”来源“:”第一邮报“,”描述“:”英国伦敦应该留在欧盟\”疣和所有\”反对党工党领袖将于周四表示,他将首次大规模干预公投活动,因为他试图反驳批评,他没有做足够的工作来说服选民支持……,“日期”:“2016-04-14T05:10:45Z”}],“下一步”: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]。标题;
这个怎么样

JSON.stringify({"Description":"LONDON Britain should stay in the EU \"warts and all\", the opposition Labour leader will say on Thursday..."}).replace(/\\/g, "\\\\")
而不是

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);
试试这个:

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"}};

它将是JSON对象。

您最终想要完成什么?为什么不将
JSON
字符串用
{}
包围以使其成为有效的JSON,然后使用
JSON.parse
按原样解析?实际的代码有{}围绕json代码。json=“…”是我遇到问题的部分。@AmphetaMachine我应该发布整个json字符串吗?@AmphetaMachine在任何情况下,如果我使用json.parse(),它都不起作用,Bing返回了无效的json?有趣的是…我这样做了:dfe=json.stringify(json).replace(/\\\/g,\\\\\\\”);obj=json.parse(dfe);document.getElementById(“demo”).innerHTML=obj.d.results[0]。Title;它不起作用。@Raul Fernandez然后呢?如果我这样做,然后尝试显示obj.d.results[0]。Title,它不起作用,那么
json
变量将是对象本身。尝试使用
json.d.results[0]
API使用
applicationon/JSON
标题以
JSON
对象格式返回数据。因此,您的响应是对象而不是字符串。欢迎光临,我很高兴为您提供帮助。