2011-08-06 80 views
1

我正在編寫將使用AJAX請求從服務器獲取一些HTML代碼以嵌入的代碼,但我似乎無法獲得正確的JSON格式。誰能告訴我我做錯了什麼?在JSON中處理html代碼

{"response": 
[ 
    {"code": "<div id=\」sponsors\」 class=\」infoBox\」 > <div class=\」title\」>THANK YOU 2011 SPONSORS!</div> <div class=\」section\」> <div class=\」party\」><a href=\」http://www.ChickRussell.com\」>Chick Russell Communications</a></div> <div class=\」cityState\」>Pasadena, California</div> <div class=\」description\」> Chick Russell Communications is a story-driven creative development company, not a design-driven company. It's one of our main distinguishing features. It doesn't matter how great it looks if it doesn't create the desired effect. <a href=\」/vendors/info/17280\」>more...</a> </div> <div class=\」web\」><a href=\」http://www.ChickRussell.com\」>www.ChickRussell.com</a></div> </div> </div>" 
    } 
] 
} 

當我嘗試運行它JSON.parse(),我得到一個語法錯誤

這裏是我用來讀取JSON代碼:

<script language="JavaScript" type="text/javascript"> 
var newxhr = new XMLHttpRequest() 

newxhr.onreadystatechange = getResponse 

function getResponse(){ 
    if(newxhr.readyState == 4) { 
     var response = newxhr.responseText; 

     console.log(response) 

     response = JSON.parse(response) 

     newxhr.close; 
    } 
} 

var url = "http://*.*.net/test.json"; 

newxhr.open("GET", url, true); 
newxhr.send(null) 
</script> 
+0

看起來像有效的JSON我發佈相關的代碼 –

回答

3

"是不同的字符。你需要stright報價,而不是捲曲的報價。 (告訴你的瀏覽器放大,如果你看不出區別...並且不使用可以自動轉換爲捲曲引號的編輯器(例如許多文字處理器),請使用適當的文本編輯器(我喜歡ActiveState Komodo) )。


老答案(JSON的問題中進行了修訂前):

你可能做錯的第一件事,是試圖用手工打造JSON。改用圖書館。

你做錯的第二件事是HTML編碼(嚴重)你的引號。要在JSON字符串中跳出",請將其表示爲\"

第三件事(但它可能只是你給一個糟糕的例子)正在打擾使用單個對象的數組。

當我試圖在其上運行JSON.parse()來,我得到一個語法錯誤

儘管它的問題,你有什麼是valid JSON,所以不應該發生。你似乎已經減少了你的測試用例,以至於你的問題沒有出現在它中。

+0

我只是說我的JavaScript代碼和固定的編碼是否有什麼不對勁的地方 –

+0

。。?爲響應新的JSON而更新了答案 – Quentin

0

它似乎爲我工作:http://jsfiddle.net/6PV4M/1/

然而,我逃單引號。

var str = '{"response":[{"code": "<div id=\」sponsors\」 class=\」infoBox\」 > <div class=\」title\」>THANK YOU 2011 SPONSORS!</div> <div class=\」section\」> <div class=\」party\」><a href=\」http://www.ChickRussell.com\」>Chick Russell Communications</a></div> <div class=\」cityState\」>Pasadena, California</div> <div class=\」description\」> Chick Russell Communications is a story-driven creative development company, not a design-driven company. It\'s one of our main distinguishing features. It doesn\'t matter how great it looks if it doesn\'t create the desired effect. <a href=\」/vendors/info/17280\」>more...</a> </div> <div class=\」web\」><a href=\」http://www.ChickRussell.com\」>www.ChickRussell.com</a></div> </div> </div>"} ]}'; 

var obj = JSON.decode(str); 
alert(obj["response"][0]["code"]); 
+0

在chrome開發工具中運行這個功能讓我ype error –

+0

我剛剛用chrome dev試過它,它的工作原理是...... –

+0

在Chrome控制檯中發佈來自網絡選項卡的ajax響應數據。 –

1

這是雙引號。你用」(統一:U + 8221),而不是「(Unicode:當U + 34)雖然他們看起來很相似,但它們是兩個不同的字符下方

的代碼是有效的:。

{ 
"response": [ 
    { 
     "code": "<div id=\"sponsors\" class=\"infoBox\" > <div class=\"title\">THANK YOU 2011 SPONSORS!</div> <div class=\"section\"> <div class=\"party\"><a href=\"http://www.ChickRussell.com\">Chick Russell Communications</a></div> <div class=\"cityState\">Pasadena, California</div> <div class=\"description\"> Chick Russell Communications is a story-driven creative development company, not a design-driven company. It's one of our main distinguishing features. It doesn't matter how great it looks if it doesn't create the desired effect. <a href=\"/vendors/info/17280\">more...</a> </div> <div class=\"web\"><a href=\"http://www.ChickRussell.com\">www.ChickRussell.com</a></div> </div> </div>" 
    } 
] 
} 

您可以隨時與http://jsonlint.com/檢查你的JSON代碼 - 這是一個偉大的工具