【问题标题】:JSON.parse not working with json from twitter streaming apiJSON.parse 不适用于来自 twitter 流 api 的 json
【发布时间】:2011-10-09 16:28:39
【问题描述】:

我正在使用 node.js 来使用 twitter 流 api。一切正常,除非我尝试解析我回来的 json。 这是我尝试解析的示例:

{
    "text": "NEWS Nº2559 (use google translator to read it): http://t.co/dF3ClUC",
    "in_reply_to_user_id": null,
    "in_reply_to_status_id": null,
    "favorited": false,
    "in_reply_to_status_id_str": null,
    "id_str": "93748566299918337",
    "in_reply_to_screen_name": null,
    "in_reply_to_user_id_str": null,
    "geo": null,
    "source": "web",
    "contributors": null,
    "retweeted": false,
    "retweet_count": 0,
    "entities": {
        "user_mentions": [],
        "hashtags": [],
        "urls": [
            {
                "display_url": "luxatenealibros.blogspot.com/2011/07/lux-at…",
                "indices": [
                    48,
                    67
                ],
                "expanded_url": "http://luxatenealibros.blogspot.com/2011/07/lux-atenea-news-n2559-cinderella-fables.html",
                "url": "http://t.co/dF3ClUC"
            }
        ]
    },
    "place": null,
    "coordinates": null,
    "user": {
        "favourites_count": 0,
        "profile_sidebar_fill_color": "efefef",
        "profile_image_url": "http://a0.twimg.com/profile_images/983835547/logo_LUX_ATENEA_WEBZINE_normal.JPG",
        "default_profile_image": false,
        "show_all_inline_media": false,
        "geo_enabled": false,
        "profile_background_tile": true,
        "screen_name": "LUXATENEAWEBZIN",
        "id_str": "112305851",
        "profile_link_color": "009999",
        "url": null,
        "description": "LUX ATENEA WEBZINE\u000d\u000aREVISTA CULTURAL GÓTICA ATIS&NYD\u000d\u000a",
        "follow_request_sent": null,
        "statuses_count": 3027,
        "verified": false,
        "profile_sidebar_border_color": "eeeeee",
        "time_zone": null,
        "contributors_enabled": false,
        "profile_use_background_image": true,
        "location": "",
        "is_translator": false,
        "lang": "es",
        "profile_background_image_url_https": "https://si0.twimg.com/images/themes/theme14/bg.gif",
        "profile_background_color": "131516",
        "protected": false,
        "listed_count": 2,
        "profile_background_image_url": "http://a1.twimg.com/images/themes/theme14/bg.gif",
        "friends_count": 3,
        "followers_count": 55,
        "name": "LUX ATENEA WEBZINE",
        "notifications": null,
        "created_at": "Mon Feb 08 00:53:45 +0000 2010",
        "id": 112305851,
        "default_profile": false,
        "following": null,
        "utc_offset": null,
        "profile_text_color": "333333",
        "profile_image_url_https": "https://si0.twimg.com/profile_images/983835547/logo_LUX_ATENEA_WEBZINE_normal.JPG"
    },
    "truncated": false,
    "id": 93748566299918340,
    "created_at": "Wed Jul 20 18:26:14 +0000 2011"
}

jsonlint.com 告诉我它是有效的 json,但无法从 node.js 解析它。知道为什么吗?

【问题讨论】:

    标签: javascript json node.js twitter


    【解决方案1】:

    我注意到了

    "id_str": "93748566299918337",
    

    和

    "id":      93748566299918340,
    

    似乎是同一数据的两种不同表示,但数字形式似乎失去了一些精度。

    JSON 数字解析器是否有可能检测到由于 ID 数字字面量正好违反尾数限制而导致的精度损失?

    JSON 实际上并没有为数字指定任何语义,也没有指定数字解析器的有损程度,但实现可能会放弃它们无法表示的数字。

    例如只有可以使用良好的 bigint/bigdecimal 表示的 JSON 解析器,例如 python,才能对 { "foo": 1e500 } 做一些合理的事情,而 JavaScript JS 解析器(使用其本机数字类型表示数字)可能会将那个数字转换为 @ 987654326@ 不能通过 JSON 往返。

    RFC 4627 第 4 节说

    4 个解析器

    ... 实现可能会设置数字范围的限制。

    编辑:

    我注意到的另一条线索在

    "text": "NEWS Nº2559 ...",
                   ^
    

    包含非 ASCII 字符。如果您使用的是 Node.js,并且您在未指定正确编码的情况下打开文件,则 JSON 解析器可能会假定 UTF-8,因为 RFC 4627 表示

    3 编码

    JSON 文本应以 Unicode 编码。默认编码是 UTF-8。

    如果您的文件不是 UTF-8,那么这可能会导致在 UTF-8 中无效的字节序列必须被解码器拒绝。

    【讨论】:

      【解决方案2】:

      我发现了问题,它来自 user.description 部分和字符 \u000d & \u000a。 以下是我如何让它发挥作用:

      var test = '{"text": "NEWS Nº2559 (use google translator to read it): http://t.co/dF3ClUC","in_reply_to_user_id": null,"in_reply_to_status_id": null,"favorited": false,"in_reply_to_status_id_str": null,"id_str": "93748566299918337","in_reply_to_screen_name": null,"in_reply_to_user_id_str": null,"geo": null,"source": "web","contributors": null,"retweeted": false,"retweet_count": 0,"entities": {"user_mentions": [],"hashtags": [],"urls": [{"display_url": "luxatenealibros.blogspot.com/2011/07/lux-at…","indices": [48,67],"expanded_url": "http://luxatenealibros.blogspot.com/2011/07/lux-atenea-news-n2559-cinderella-fables.html","url": "http://t.co/dF3ClUC"}]},"place": null,"coordinates": null,"user": {"favourites_count": 0,"profile_sidebar_fill_color": "efefef","profile_image_url": "http://a0.twimg.com/profile_images/983835547/logo_LUX_ATENEA_WEBZINE_normal.JPG","default_profile_image": false,"show_all_inline_media": false,"geo_enabled": false,"profile_background_tile": true,"screen_name": "LUXATENEAWEBZIN","id_str": "112305851","profile_link_color": "009999","url": null,"description": "LUX ATENEA WEBZINE\u000d\u000aREVISTA CULTURAL GÓTICA ATIS&NYD\u000d\u000a","follow_request_sent": null,"statuses_count": 3027,"verified": false,"profile_sidebar_border_color": "eeeeee","time_zone": null,"contributors_enabled": false,"profile_use_background_image": true,"location": "","is_translator": false,"lang": "es","profile_background_image_url_https": "https://si0.twimg.com/images/themes/theme14/bg.gif","profile_background_color": "131516","protected": false,"listed_count": 2,"profile_background_image_url": "http://a1.twimg.com/images/themes/theme14/bg.gif","friends_count": 3,"followers_count": 55,"name": "LUX ATENEA WEBZINE","notifications": null,"created_at": "Mon Feb 08 00:53:45 +0000 2010","id": 112305851,"default_profile": false,"following": null,"utc_offset": null,"profile_text_color": "333333","profile_image_url_https": "https://si0.twimg.com/profile_images/983835547/logo_LUX_ATENEA_WEBZINE_normal.JPG"},"truncated": false,"id": 93748566299918340,"created_at": "Wed Jul 20 18:26:14 +0000 2011"}';
      
      test = test.replace(/\n/g, '');
      test = test.replace(/\r/g, '');
      
      console.log(JSON.parse(test));
      

      【讨论】:

        猜你喜欢
        • 2012-01-23
        • 2013-02-28
        • 2023-03-18
        • 2014-03-29
        • 1970-01-01
        • 1970-01-01
        • 2011-05-11
        • 1970-01-01
        • 2021-10-18
        相关资源
        最近更新 更多