我使用jQuery的$ .getJSON检索鸣叫和未知原因,一些推特ID(S)正在四舍五入 "id": 34903960418516968,
到"id": 34903960418516970,
所以不是我想的列表使用$ .ajax和dataType:“text”,然后通过转换为JSON,parseJSON,JSON.parse和eval不起作用。 我发现的唯一解决方案是在JSON源仍处于文本格式的情况下,创建ID字符串的数据类型而不是Number。我包围了实际的ID用双引号“,我用下面的正则表达式来做到这一点。JQuery的parseJSON分享Tweet ID围捕问题和解决方案
data = data.replace(/\"id\": (\d{15,}),/gi, "\"id\": \"$1\",");
Ajax调用
$.ajax({
async: true,
url: tweetsUrl,
dataType: "text",
success: getTweetsResult
});
的getTweetsResult
function getTweetsResult(data, textStatus, xhr){
//remove trailing ,
data = data.substring(0, data.lastIndexOf(",]")) + "]";
//convert tweet.id to string because the eval of JS is rounding up some of the number
data = data.replace(/\"id\": (\d{15,}),/gi, "\"id\": \"$1\",");
//console works with Chrome
// console.log(data);
var tweetList = $.parseJSON(data);
generateTweets("tweets", "tweetGrid", tweetList, 3, tweets);
applyRollOverGridItems();
}
双重检查我发现JSON中的推文确实包含名为id_str的字符串中的id字段。谢谢。 – Charbel 2011-02-11 10:32:27