解析有效 JSON 时出现“未捕获的语法错误:意外令牌:”

'Uncaught SyntaxError: Unexpected token :' when parsing valid JSON

本文关键字:语法 错误 令牌 意外 JSON 有效      更新时间:2023-09-26

我正在解析来自外部 url 的 JSON 数据,但由于"未捕获的语法错误:意外的令牌:"语法错误而无法使用数据。我检查了 JSONLint 上的 JSON,它验证正常,所以我不知道我可能做错了什么。

我从这个网址得到它:

http://clipped.me/algorithm/clippedapi.php?url=http://www.bbc.com/news/world-asia-china-30067035&callback=?

像这样解析它:

$.getJSON("http://clipped.me/algorithm/clippedapi.php?url=http://www.bbc.com/news/world-asia-china-30067035&callback=?", function(data) {
    var story = data.summary[0];
    console.log(story);
    $('p').html(story)
});

收到的数据是这样的:

{
    "title": "BBC News - Hong Kong protest leaders denied Beijing flight",
    "summary": [
        "They had hoped to meet China's leaders as part of their push for greater democracy, but were told at the airport that their travel permits were invalid.",
        "They want Beijing to allow more candidates to stand in the territory's next leadership election in 2017.",
        "The group were greeted at the airport by fellow democracy activists, who unfurled yellow umbrellas - a symbol of Hong Kong's democracy movement."
    ],
    "source": "bbc.com"
}

下面是一个 JSFIDDLE 示例。

看起来 clipped.me 忽略了你的回调,只是将原始JSON转储到DOM中,而不是格式正确的JSONP回调。失败时,将生成语法错误消息。他们的 API 也不支持跨源。看起来它被设计为仅从服务器端代码使用,而不是客户端 JS。

另请参阅此处 - $.getJSON 解析器尝试调用 API 时出错