2016-04-14 2 views
0

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; 
+0

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

+0

Le code réel a {} entourant le code json. Le json = '...' est la partie dans laquelle j'ai un problème. @amphetamachine – grindel

+0

Devrais-je publier toute la chaîne json? @amphetamachine – grindel

Répondre

0

Au lieu de

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

Essayez ceci:

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

Ce sera l'objet JSON.

+0

Alors quoi? Si je fais cela, puis essayez d'afficher obj.d.results [0].Titre, cela ne fonctionne pas – grindel

+0

Ensuite, votre variable 'json' sera l'objet lui-même. Essayez d'utiliser 'json.d.results [0]' –

+0

L'API retourne les données au format objet 'JSON' en utilisant l'en-tête' applicaiton/json'. Donc, votre réponse est l'objet et non la chaîne. –

0

Qu'en est-ce?

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

J'ai fait ceci: dfe = JSON.stringify (json) .replace (/ \\/g, "\\\\"); obj = JSON.parse (dfe); document.getElementById ("demo"). InnerHTML = obj.d.results [0] .Titre; Cela n'a pas fonctionné. @Raul Fernandez – grindel