使用Javascript解析Twitter Json文本

我需要帮助解析从Twitter返回的JSON提要文本。 我需要访问,并将样式标记应用于链接,created_date和其他信息。 有关如何完成此任务的任何提示? 提前致谢

谷歌的第一个结果:

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("
  • " + item.text.linkify() + " " + relative_time(item.created_at) + " via " + item.source + "
  • "); }); });

    和HTML:

     

      另一个例子。 使用jQuery和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 = ' 

      在服务器端解析要容易得多,但我猜你是在做客户端的网站吗?

      示例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; 

      POW

      看看$ .json ,它是专门为此而制作的。 它是一个ajax调用,它在返回(进入数组)时自动解析json以在回调中使用。

      如果你想将JSON转换为HTML,有一个很好的模板引擎: tempo js

      从客户端而不是服务器端访问Twitter API是有充分理由的。 如果您使用PHP访问服务器端的API,则服务器的IP可能会受到Twitter的速率限制。 此外,似乎Twitter可能没有公布的费率限制。

      使用REST API无济于事,因为限制太低,无法为未知计数(可能是大量用户)开发站点。 这不可扩展。

      使用JavaScript可能更容易让客户端请求数据。

      OAuth每个客户端都可以使用他/她自己的API限制,但是获取一些推文真是令人头疼。 我认为,通用使用是一种更容易绕过的方式。