简体   繁体   English

JSON.parse无法与Twitter Streaming API中的JSON一起使用

[英]JSON.parse not working with json from twitter streaming api

I'm using node.js to use the twitter streaming api. 我正在使用node.js来使用Twitter流式API。 Everything works fine except when I try to parse the json I get back. 一切正常,除了当我尝试解析json时。 Here is an example of what I try to parse : 这是我尝试解析的示例:

{
    "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 tells me that it is valid json but it's impossible to parse it from node.js. jsonlint.com告诉我,它是有效的json,但无法从node.js进行解析。 Any idea why ? 知道为什么吗?

I noticed that 我注意到

"id_str": "93748566299918337",

and

"id":      93748566299918340,

seem to be two different representations of the same data, but the number form seems to have lost some precision. 似乎是同一数据的两种不同表示形式,但是数字形式似乎失去了一些精度。

Is it possible that the JSON number parser is detecting a loss of precision due to the ID number literal being right up against the limit of the mantissa and bails on that? JSON数字解析器是否有可能检测到精度损失,因为ID数字文字正好符合尾数和保释标准?

JSON doesn't actually specify any semantics for numbers, and doesn't specify how lossy number parsers can be, but implementations might bail on numbers they can't represent. JSON实际上并未为数字指定任何语义,也没有指定有损数字解析器的方式,但是实现可能会针对无法代表的数字提供保释。

Eg only a JSON parser that can use a good bigint/bigdecimal representation, like python's, will be able to do something reasonable with { "foo": 1e500 } whereas a JavaScript JS parser (that represents numbers using its native number type) would probably turn that number into Infinity which is not round-trippable via JSON. 例如,只有可以使用良好的bigint / bigdecimal表示形式(例如python的JSON解析器)的JSON解析器才能使用{ "foo": 1e500 }做一些合理的事情,而JavaScript JS解析器(使用其本机数字类型表示数字)则可能将该数字转换为Infinity ,该数字不能通过JSON往返。

Section 4 of RFC 4627 says RFC 4627的第4节说

4 Parsers 4解析器

... An implementation may set limits on the range of numbers. ...实现可能会限制数字范围。

EDIT: 编辑:

The other clue I notice is in 我注意到的另一个线索是

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

which contains a non-ASCII character. 包含非ASCII字符。 If you're using Node.js and you're opening a file without specifying the correct encoding, the JSON parser might be assuming UTF-8 since RFC 4627 says 如果您使用的是Node.js,并且在打开文件时未指定正确的编码,那么JSON解析器可能会采用UTF-8,因为RFC 4627表示

3 Encoding 3编码

JSON text SHALL be encoded in Unicode. JSON文本应以Unicode编码。 The default encoding is UTF-8. 默认编码为UTF-8。

and if your file is not UTF-8 then that might lead to a byte sequence that is not valid in UTF-8 which would have to be rejected by the decoder. 如果文件不是UTF-8,则可能会导致字节序列在UTF-8中无效,因此解码器必须拒绝该字节序列。

I have found the problem, it comes from the user.description part and the characters \ & \ . 我发现了问题,它来自user.description部分以及字符\\ u000d和\\ u000a。 Here is how I did to make it work : 这是我使它工作的方法:

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM