【发布时间】:2011-09-24 00:15:07
【问题描述】:
我在解析从 Twitter 返回的 JSON 提要文本方面需要帮助。我需要访问并将样式标签应用于链接、created_date 和其他信息。关于如何做到这一点的任何提示?提前致谢
【问题讨论】:
-
到目前为止你尝试过什么?如果您展示您的代码,社区可以为您提供帮助。
-
听起来他正在寻找一个开始的地方,因此还没有任何东西。
我在解析从 Twitter 返回的 JSON 提要文本方面需要帮助。我需要访问并将样式标签应用于链接、created_date 和其他信息。关于如何做到这一点的任何提示?提前致谢
【问题讨论】:
谷歌上的第一个结果:
Ralph Whitbeck - Blog - Pulling twitter updates with JSON and jQuery。代码如下:
var url = "http://twitter.com/status/user_timeline/RedWolves.json?count=3&callback=?";
$.getJSON(url, function(data){
$.each(data, function(i, item) {
$("img#profile").attr("src", item.user["profile_image_url"]);
$("#tweets ul").append("<li>"
+ item.text.linkify()
+ " <span class='created_at'>"
+ relative_time(item.created_at)
+ " via "
+ item.source
+ "</span></li>");
});
});
还有html:
<div id="tweets">
<img id="profile">
<ul></ul>
</div>
另一个例子。 Fetching tweets with jQuery and the Twitter JSON API。转载如下:
$(document).ready(function() {
// Declare variables to hold twitter API url and user name
var twitter_api_url = 'http://search.twitter.com/search.json';
var twitter_user = 'lupomontero';
// Enable caching
$.ajaxSetup({ cache: true });
// Send JSON request
// The returned JSON object will have a property called "results" where we find
// a list of the tweets matching our request query
$.getJSON(
twitter_api_url + '?callback=?&rpp=5&q=from:' + twitter_user,
function(data) {
$.each(data.results, function(i, tweet) {
// Uncomment line below to show tweet data in Fire Bug console
// Very helpful to find out what is available in the tweet objects
//console.log(tweet);
// Before we continue we check that we got data
if(tweet.text !== undefined) {
// Calculate how many hours ago was the tweet posted
var date_tweet = new Date(tweet.created_at);
var date_now = new Date();
var date_diff = date_now - date_tweet;
var hours = Math.round(date_diff/(1000*60*60));
// Build the html string for the current tweet
var tweet_html = '<div class="tweet_text">';
tweet_html += '<a href="http://www.twitter.com/';
tweet_html += twitter_user + '/status/' + tweet.id + '">';
tweet_html += tweet.text + '<\/a><\/div>';
tweet_html += '<div class="tweet_hours">' + hours;
tweet_html += ' hours ago<\/div>';
// Append html string to tweet_container div
$('#tweet_container').append(tweet_html);
}
});
}
);
});
【讨论】:
在服务器端解析会容易得多,但我猜你是在完全在客户端做网站?
示例 Javascript:
//store your JSON into a variable
var yourJSON = {"animals":
[ {"type": "dog", "name": "Paul"},
{"type": "cat", "name": "Ralph"},
{"type": "bird", "name": "Jim"} ]
};
//retrieve data and store into variables (do with them what you will)
var PaulsType = yourJSON.animals[0].type; //returns 'dog'
var BirdsName = yourJSON.animals[2].name; //returns 'Jim'
所以对于Twitter,有很多层级的封装,所以你需要做相应的调整。例如,为了吸引您的关注者,您将拥有如下内容:
[{"statuses_count":527,"profile_use_background_image":true, ....
....
,"status":{"place":null,"retweeted_status": {"place":null,"coordinates":null,"retweet_count":"100+","truncated":false ,"text":"BLAHBLAHBLAH" .....
所以这只是显示索引 0。如果你想返回你的第一个关注者最近的推文的文本(最近关注你的人),你必须像这样使用 javascript .在此示例中,推文是转推(以显示封装的使用):
var yourJSON = {put Twitter output here};
var firstFollowersTweet_retweet = yourJSON[0].status.retweeted_status.text;
//to get raw text whether retweet or not
var firstFollowersTweet = yourJSON[0].status.text;
战俘
【讨论】:
看$.json,就是专门为此制作的。这是一个 ajax 调用,它会在返回时自动解析 json(到一个数组中)以在回调中使用。
【讨论】:
如果你想将 JSON 转换为 HTML,有一个不错的模板引擎:tempo js
【讨论】:
从客户端而不是服务器端访问 Twitter API 是有充分理由的。如果您使用 PHP 在服务器端访问他们的 API,服务器的 IP 可能会受到 Twitter 的速率限制。此外,Twitter 似乎没有发布速率限制。
使用 REST API 无济于事,因为限制太低,无法为未知数量(可能很大)的用户开发网站。这是不可扩展的。
使用 JavaScript 可能更容易让客户端请求数据。
可以对每个客户端进行 OAuth 并使用他/她自己的 API-Limit,但只是为了获取一些推文而令人头疼。我认为,泛型使用是一种更容易绕过的方式。
【讨论】: