I'm passing back a list of approved tweets from a webserver in JSON format. When I go to the URL: http://localhost:8000/showtweets/?after_id=354210796420608003
in my browser I get the following JSON:
[{
"date": "2013-07-08T12:10:09",
"text": "#RaspberryPi ist auf dem Weg :-)",
"author_pic_url": "http://a0.twimg.com/profile_images/1315863231/twitter_normal.jpg",
"id": 354210796420608004,
"author": "switol"
}]
Which has an id of: 354210796420608004
.
When I make a GET call from Javascript, the number changes:
function TweetUpdater() {
}
TweetUpdater.latest_id = 0;
TweetUpdater.undisplayed_tweets = new Array();
TweetUpdater.prototype.get_more_tweets = function () {
// var since_id = parseFloat(TweetUpdater.get_latestlatest_id;
// alert(since_id);
var get_tweets_url = "/showtweets/?after_id="+TweetUpdater.latest_id;
$.get(get_tweets_url, function (tweets) {
if (tweets.length > 0) {
/////////////////////////////////////////////////////////
alert(tweets[0].id+", "+ tweets[0].text); <<<<< THIS LINE
/////////////////////////////////////////////////////////
TweetUpdater.latest_id = tweets[0].id;
for (var i = 0; i < tweets.length; i++) {
TweetUpdater.undisplayed_tweets.push(tweets[i]);
}
}
}, "json");
};
This code alerts: 354210796420608000, #RaspberryPi ist auf dem Weg :-)
354210796420608004 != 354210796420608000
Very odd.
The "SyntaxError: JSON. parse: unexpected character" error occurs when passing a value that is not a valid JSON string to the JSON. parse method, e.g. a native JavaScript object. To solve the error, make sure to only pass valid JSON strings to the JSON.
The default is 2097152 characters, which is equivalent to 4 MB of Unicode string data. JSON itself has no inherent limit.
For example, a 64-bit integer cannot be represented in JSON (since JavaScript and JSON support integers up to 2^53). Therefore, a 64-bit integer must be represented as a string in JSON requests/responses.
Valid Data Types In JSON, values must be one of the following data types: a string. a number. an object (JSON object)
No, not very odd. JS represents all numbers as double, and with growing integers you loose precision at some point. See What is JavaScript's highest integer value that a Number can go to without losing precision? for details.
To solve the problem, simply make the id
a string - you're not doing calculations with it anyway. You'll have to do that in the original JSON though, otherwise the precision loss happens at JSON.parse
already.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With