J'utilise json result de l'API Bing Search. Dans le résultat, les guillemets doubles sont échappés par une seule barre oblique inverse. Javascript cependant, n'accepte pas ceci. Cela nécessite que j'échappe aux doubles guillemets en utilisant un double antislash. Donc, ma question est de savoir comment remplacer le backslash unique avec le double backslash. Par exemple, une partie du code JSON est comme ceRemplacer l'échappement backslash unique avec double barre oblique inverse en JavaScript
"Description":"LONDON Britain should stay in the EU \"warts and all\", the opposition Labour leader will say on Thursday..."
Je voudrais que ce soit comme ça
"Description":"LONDON Britain should stay in the EU \\"warts and all\\", the opposition Labour leader will say on Thursday..."
J'ai essayé la solution suivante
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,'\\\\"');
Cependant, il didn ne fonctionne pas. Il a remplacé toutes les barres obliques inverses avant toutes les guillemets doubles. Il est passé de cette ...
\"Description\":\"LONDON Britain should stay in the EU \"warts and all\", the opposition Labour leader will say on Thursday...\"
... à ce
\\"Description\\":\\"LONDON Britain should stay in the EU \\"warts and all\\", the opposition Labour leader will say on Thursday...\\"
Quelqu'un peut-il me dire comment remplacer \ "avec \\"?
Edit: Ce que je veux faire est ce
<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;
Qu'essayez-vous finalement d'accomplir? Pourquoi ne pas simplement entourer votre chaîne 'json' avec' {} 'pour la rendre valide JSON, puis analyser comme-est en utilisant' JSON.parse'? – amphetamachine
Le code réel a {} entourant le code json. Le json = '...' est la partie dans laquelle j'ai un problème. @amphetamachine – grindel
Devrais-je publier toute la chaîne json? @amphetamachine – grindel