2012-06-18 47 views
1

我一直在嘗試,但無法弄清楚爲什麼這個JSON無效(當我把它放在www.jsonlint.com上)任何人都可以幫忙嗎?這裏是JSON:爲什麼這個JSON無效?

[ 
    { 
     "title": "COFA NEW SOUTH WALES ", 
     "img": "images/uni/cofa.png", 
     "desc": "One of the Best University", 
     "url": "http://127.0.0.1:8888/ilets/app/col.html" 
    }, 
    { 
     "title": "MIDDLESEX UNIVERSITY ", 
     "img": "images/1339977840.jpg", 
     "desc": "Well this is Testi", 
     "url": "http://127.0.0.1:8888/ilets/app/MIDDLESEX UNIVERSITY.html" 
    }, 
    { 
     "title": "ROYAL COLLEGE OF ARTS ", 
     "img": "images/uni/1339978144.png", 
     "desc": "This is One of the best University", 
     "url": "http://127.0.0.1:8888/ilets/app/ROYAL COLLEGE OF ARTS.html" 
    }, 
    { 
     "title": "UNIVERSITY OF SCOTLAND ", 
     "img": "images/uni/1339996882.gif", 
     "desc": "SCOTLAND OWNS THIS UNIVERSITY", 
     "url": "http://127.0.0.1:8888/ilets/app/UNIVERSITY OF SCOTLAND.html" 
    } 
] 
+0

這似乎驗證了我的JSONlint.com –

+0

我複製並粘貼您的樣品放入JSONlint其說「有效的JSON」 –

+3

[JSONLint](http://jsonlint.com )說這是有效的。 – Filburt

回答

0

好吧,這是奇怪的。這裏生成JSON:syedmujahidali.com/ilets/retrive.php從這裏複製粘貼到JSONlint,它說無效。

只從該頁面(見Darin's answer爲什麼我作出這樣的區分)響應的顯示的文本來看,問題是,你的迴應與BOM打開。 BOM使JSON無效。我去了那個鏈接,做了一個選擇全部和複製,然後粘貼到jsonlint.com,並得到了「無效令牌」的錯誤,你會得到。所以我把它粘貼到vim中,vim在文本的開頭給我看了FE FF。

所以修復的方法是找出PHP頁面爲什麼以BOM開始並解決該問題。您不需要使用UTF-8的物料清單。

+0

什麼BOM?這個PHP腳本返回HTML,而不是JSON。如果OP試圖直接解析來自這個PHP腳本的響應,他將失敗,因爲腳本沒有返回JSON。 –

+0

@DarinDimitrov:對,如果他試圖直接處理該頁面的完整回覆。但我認爲他是指它返回的文本,其中包含BOM。您可能想要將HTML方面發佈爲答案。 *編輯*:啊,你有。 :-) +1 –

2

您在問題中顯示的JSON是有效的。另一方面,您在註釋部分(http://www.syedmujahidali.com/ilets/retrive.php)中提到的腳本將返回HTML,而不是JSON,因此如果嘗試使用JSON解析器解析此腳本的結果,您顯然會失敗。這裏的響應的樣子:

<head> 

<meta charset="UTF-8"></head><body onload="abc()"> 
<script> 
function abc(){ 
document.getElementById('copy').innerHTML = document.getElementById('content').innerHTML; 
var ab= document.getElementById('content').innerHTML; 
} 
</script> 

<div id="content" style="display:none;"> 


[{"title":"COFA NEW SOUTH WALES " ,"img":"images/uni/cofa.png","desc":"One of the Best University","url":"http://127.0.0.1:8888/ilets/app/col.html"},<br>{"title":"MIDDLESEX UNIVERSITY " ,"img":"images/1339977840.jpg","desc":"Well this is Testi","url":"http://127.0.0.1:8888/ilets/app/MIDDLESEX UNIVERSITY.html"},<br>{"title":"ROYAL COLLEGE OF ARTS " ,"img":"images/uni/1339978144.png","desc":"This is One of the best University","url":"http://127.0.0.1:8888/ilets/app/ROYAL COLLEGE OF ARTS.html"},<br>{"title":"UNIVERSITY OF SCOTLAND " ,"img":"images/uni/1339996882.gif","desc":"SCOTLAND OWNS THIS UNIVERSITY","url":"http://127.0.0.1:8888/ilets/app/UNIVERSITY OF SCOTLAND.html"}]</div> 

<div id="copy"> 

</div> 



</body>