2016-04-14 512 views
0

我使用必應搜索API的json結果。在結果中,雙引號會被一個反斜槓轉義。但是,Javascript不接受這一點。它要求我使用雙反斜槓來避免雙引號。 所以,我的問題是,如何用雙反斜槓替換單個反斜槓。 例如,JSON代碼的一部分是這樣用JavaScript中的雙反斜槓替換單個反斜槓

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

你最終想要完成什麼?爲什麼不用''}環繞'json'字符串以使其成爲有效的JSON,然後使用'JSON.parse'解析原樣? – amphetamachine

+0

實際的代碼{}圍繞着json代碼。 json ='...'是我遇到問題的部分。@amphetamachine – grindel

+0

我應該發佈整個json字符串嗎? @amphetamachine – grindel

回答

0

而不是

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對象。

+0

然後呢? 如果我這樣做,然後嘗試顯示obj.d.results [0]。標題,它不起作用 – grindel

+0

然後你的'json'變量將是對象本身。嘗試使用'json.d.results [0]' –

+0

API使用'applicaiton/json'頭以'JSON'對象格式返回數據。所以你的迴應是對象而不是字符串。 –

0

這個是什麼?

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

我這樣做過: dfe = JSON.stringify(json).replace(/ \\/g,「\\\\」); obj = JSON.parse(dfe); document.getElementById(「demo」)。innerHTML = obj.d.results [0] .Title; 它沒有工作。 @Raul Fernandez – grindel

相關問題