2011-06-28 46 views
1

我需要幫助解析從Twitter返回的JSON供稿文本。我需要訪問並將樣式標記應用於鏈接,created_date和其他信息。有關如何完成此任何提示?在此先感謝使用Javascript解析Twitter Json文本

+0

你到目前爲止嘗試過什麼?如果你顯示你的代碼,社區可以幫助你。 – Roger

+0

聽起來像他正在尋找一個地方開始,因此沒有任何事情。 – Ben

回答

3

在谷歌的結果:

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); 
     } 
     }); 
    } 
); 
}); 
+0

我試過拉爾夫惠特貝克解決方案(以及我能夠直接從頁面複製樣本),但無法讓我的案件工作。我想我喜歡第二個,並且一定會讓你知道如果這個作品 – Kobojunkie

0

看看$.json,它是專門爲此。這是一個ajax調用,它會自動解析返回時的json(到數組中)以用於回調。

0

如果你想JSON轉換爲HTML,有一個很好的模板引擎:tempo js

1

這將是更容易解析在服務器端,但我猜你正在做的網站完全客戶端?

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」:真實,...
....
, 「狀態」: {「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

+0

非常感謝。 – Kobojunkie

-1

有一個很好的理由來訪問從客戶端,而不是服務器端Twitter的API 。如果您使用PHP在服務器端訪問其API,則服務器的IP可能會受到Twitter的限速。此外,Twitter似乎沒有公佈費率限制。

使用REST API將無濟於事,因爲限制太低而無法爲未知數(可能是大數目)的用戶開發網站。這不可擴展。

使用JavaScript可能會讓客戶端更容易地請求數據。

這將是可能OAuth每個客戶端和使用他/她自己的API限制,但多麼令人頭疼只是爲了得到一些推文。我認爲,通用使用是一種更容易繞過的方式。

+0

這不是一個答案。既然你提供了可能有用的建議,它應該只是OP下的評論。 – methai