2011-07-20 17 views
0

我使用node.js來使用twitter流api。一切工作正常,除非我試圖解析我回來的JSON。 這裏是什麼,我嘗試解析一個例子:JSON.parse與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中解析它任何想法爲什麼?

回答

0

我發現這個問題,它來自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)); 
4

我注意到,

"id_str": "93748566299918337", 

"id":  93748566299918340, 

似乎是相同的數據的兩個不同的表現,但數量形式似乎已失去了一些精度。

是否有可能由於ID號字符串正好對尾數的限制而導致JSON數字解析器檢測到精度損失?

JSON實際上並沒有爲數字指定任何語義,也沒有指定有損數字解析器的可能性,但實現可能會保留在他們無法表示的數字上。

例如只有一個可以使用bigint/bigdecimal表示的JSON解析器(如python的)可以用{ "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中無法使用的字節序列,該序列將不得不被解碼器拒絕。