2016-04-14 23 views
0

Bing Search API'sından json sonucu kullanıyorum. Sonuçta, çift tırnak tek bir ters eğik çizgi tarafından kaçtı. Ancak Javascript bunu kabul etmiyor. Çift ters eğik çizgi kullanarak çift tırnaktan kaçmamı gerektirir. Yani, sorum şu ki, tek ters eğik çizgiyi çift ters eğik çizgiyle nasıl değiştirebilirim? Örneğin, json bir kod parçası, buTek ters eğik çizgi kaçışını, çift ters eğik çizgi ile değiştirin.

"Description":"LONDON Britain should stay in the EU \"warts and all\", the opposition Labour leader will say on Thursday..." 

gibi I bu

"Description":"LONDON Britain should stay in the EU \\"warts and all\\", the opposition Labour leader will say on Thursday..." 

gibi istiyorum I

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,'\\\\"'); 

Ancak aşağıdaki çözüm çalıştı, bu değil mi Çalışıyorum Tüm çift tırnaklardan önce tüm ters eğik çizgileri değiştirdi. Herhangi birinin nasıl yerine \ "\\ ile" söyleyebilir bu

\\"Description\\":\\"LONDON Britain should stay in the EU \\"warts and all\\", the opposition Labour leader will say on Thursday...\\" 

için ... ... bundan

\"Description\":\"LONDON Britain should stay in the EU \"warts and all\", the opposition Labour leader will say on Thursday...\" 

gitti?

Düzenleme: Ne yapmak istiyorum bu konuda bu

<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

Nihai olarak neyi başarmaya çalışıyorsunuz? Neden sadece 'json' dizgisini geçerli JSON yapmak için '{}' ile çevrelemeyin, sonra da JSON.parse'yi kullanarak ayrıştırılamıyorsunuz? – amphetamachine

+0

Gerçek kod, json kodunu çevreleyen {} öğeye sahiptir. Json = '...' bir sorunum var kısmıdır. @amphetamachine – grindel

+0

Tüm json dizisini yayınlamalı mıyım? @amphetamachine – grindel

cevap

0

yerine

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

bu deneyin:

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

Bu JSON nesnesi olacaktır.

+0

O zaman ne? Bunu yaparsam ve obj.d.results [0] görüntülemeye çalışır.Başlık, işe yaramıyor – grindel

+0

Sonra 'json' değişkeniniz nesneyi kendisi olacaktır. Kullanmaya çalışın json.d.results [0] ' –

+0

API,' applicaiton/json 'başlığını kullanarak 'JSON' nesne biçimindeki verileri döndürür. Yani cevabın nesne ve dize değil. –

0

ne olduğunu?

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

Bunu yaptım: dfe = JSON.stringify (json) .replace (/ \\/g, "\\\\"); obj = JSON.parse (dfe); document.getElementById ("demo"). InnerHTML = obj.d.results [0] .Title; Çalışmıyordu. @Raul Fernandez – grindel

İlgili konular