Я использую результат json из Bing Search API. В результате двойные кавычки экранируются одной обратной косой чертой. Однако 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;
json
{}
, чтобы сделать ее допустимой JSON, а затем проанализировать как есть, используяJSON.parse
? - person amphetamachine   schedule 14.04.2016