0

我一直在尝试但无法弄清楚为什么这个 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"
    }
]
4

2 回答 2

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>
于 2012-06-18T06:48:43.943 回答
0

好吧,这很奇怪。JSON 在这里生成:syedmujahidali.com/ilets/retrive.php 从这里复制粘贴到 JSONlint 上,它说无效。

仅查看来自该页面的响应的显示文本(请参阅Darin 的回答了解我为何做出这种区分),问题是您的响应以BOM打开。BOM 使 JSON 无效。我去了那个链接,全选并复制,然后将其粘贴到 jsonlint.com,并得到了你得到的“无效令牌”错误。于是我把它粘贴到vim中,vim给我看了正文开头的FE FF。

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

于 2012-06-18T06:44:56.070 回答