开发者

Twitter JSON in_reply_to_status_id wrong

开发者 https://www.devze.com 2023-03-12 13:13 出处:网络
Has anybody ever had problems with twitters json .in_reply_to_status_id It seems to be rounding my last number to 0 everytime. If you run this code it should prompt you the url of where the in reply

Has anybody ever had problems with twitters json .in_reply_to_status_id

It seems to be rounding my last number to 0 everytime. If you run this code it should prompt you the url of where the in reply to message should be, but the last number is always a 0. I am pretty new to javascript but think it might be something with how big of a number it is. Any help appreciated. Thanks :)

<!-- language: lang-js -->
<script type="text/javascript">
 function twitterCallback(twitters) {
  var statusHTML = [];
  for (var i=0; i<twitters.length; i++){
    var username = twitters[i].user.screen_name;
    var status = twitters[i].text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
       return '<a href="'+url+'" target="_blank" rel="nofollow">'+url+'</a>';
    }).replace(/\B@([_a-z0-9]+)/ig, function(reply) {
      return  reply.charAt(0)+'<a href="http://twitter.com/'+reply.substring(1)+'" target="_blank" rel="nofollow">'+reply.substring(1)+'</a>';
    });

    if(twitters[i].i开发者_StackOverflow社区n_reply_to_screen_name){
        alert('http://twitter.com/'+twitters[i].in_reply_to_screen_name+'/status/'+twitters[i].in_reply_to_status_id);
    }
  }
}
</script>
<script type="text/javascript" src="http://api.twitter.com/1/statuses/user_timeline.json?screen_name=PacSun&include_rts=true&count=15&callback=twitterCallback"></script>


Use the in_reply_to_status_id_str on the status object instead. Twitter's Snowflake status ID scheme produces values that are larger than the maximum integer value supported by Javascript and some JSON parsers. As a result of this, all numeric Twitter ID's that could have the potential of growing over 53 bits have *_str property complements.

From the linked document:

The problem

Before launch it came to our attention that some programming languages such as Javascript cannot support numbers with >53bits. This can be easily examined by running a command similar to: (90071992547409921).toString() in your browser's console or by running the following JSON snippet through your JSON parser.

{"id": 10765432100123456789, "id_str": "10765432100123456789"}  

In affected JSON parsers the ID will not be converted successfully and will lose accuracy. In some parsers there may even be an exception.

The solution

To allow javascript and JSON parsers to read the IDs we need to include a string version of any ID when responding in the JSON format. What this means is Status, User, Direct Message and Saved Search IDs in the Twitter API will now be returned as an integer and a string in JSON responses. This will apply to the main Twitter API, the Streaming API and the Search API.

Other links:

What is JavaScript's highest integer value that a Number can go to without losing precision?
JavaScript 64 bit numeric precision
What is the accepted way to send 64-bit values over JSON?

0

精彩评论

暂无评论...
验证码 换一张
取 消