[英]JSON.parse not working with json from twitter streaming api
我正在使用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進行解析。 知道為什么嗎?
我注意到
"id_str": "93748566299918337",
和
"id": 93748566299918340,
似乎是同一數據的兩種不同表示形式,但是數字形式似乎失去了一些精度。
JSON數字解析器是否有可能檢測到精度損失,因為ID數字文字正好符合尾數和保釋標准?
JSON實際上並未為數字指定任何語義,也沒有指定有損數字解析器的方式,但是實現可能會針對無法代表的數字提供保釋。
例如,只有可以使用良好的bigint / bigdecimal表示形式(例如python的JSON解析器)的JSON解析器才能使用{ "foo": 1e500 }
做一些合理的事情,而JavaScript JS解析器(使用其本機數字類型表示數字)則可能將該數字轉換為Infinity
,該數字不能通過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中無效,因此解碼器必須拒絕該字節序列。
我發現了問題,它來自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));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.