为什么这个JSON无效?

时间:2012-06-18 06:32:51

标签: json validation

我一直在尝试,但无法弄清楚为什么这个JSON无效(当我把它放在www.jsonlint.com上时)有人可以帮忙吗?这是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"
    }
]

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

  

好的,这很奇怪。这里生成JSON:syedmujahidali.com/ilets/retrive.php在JSONlint上从这里进行Copypaste,它表示无效。

仅查看该页面回复的显示文字(请参阅Darin's answer了解我为何区分这一点),问题是您的回复会以BOM打开。 BOM使JSON无效。我去了那个链接,做了一个选择全部并复制,然后粘贴到jsonlint.com,并得到你得到的“无效令牌”错误。所以我将它粘贴到vim中,vim在文本开头向我展示了FE FF。

所以解决方法是找出为什么PHP页面以BOM开头并修复它。您不需要带有UTF-8的BOM。