2012-06-18 114 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>