使用Javascript解析Twitter Json文本 [英] Parsing Twitter Json text using Javascript
本文介绍了使用Javascript解析Twitter Json文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要帮助解析从Twitter返回的JSON Feed文本。我需要访问,并应用样式标签到链接,created_date和其他信息。任何提示如何完成这个?提前感谢
I need help with parsing of JSON feed text returned from Twitter. I need to access, and apply style tags to the link, created_date, and other info. Any hints on how to accomplish this? Thanks in advance
推荐答案
Google上的第一个结果:
First results on google:
Ralph Whitbeck - 博客 - 使用JSON和jQuery拉取Twitter更新。以下代码:
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>
另一个例子。 使用jQuery和Twitter JSON API获取tweets 。复制如下:
Another example. Fetching tweets with jQuery and the Twitter JSON API. Reproducing below:
$(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解析Twitter Json文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文