使用Javascript解析Twitter Json文本

问题描述:

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

+0

你到目前为止尝试过什么?如果你显示你的代码,社区可以帮助你。 – Roger

+0

听起来像他正在寻找一个地方开始,因此没有任何事情。 – Ben

在谷歌的结果:

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

看看$.json,它是专门为此。这是一个ajax调用,它会自动解析返回时的json(到数组中)以用于回调。

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

这将是更容易解析在服务器端,但我猜你正在做的网站完全客户端?

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

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

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

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

这将是可能OAuth每个客户端和使用他/她自己的API限制,但多么令人头疼只是为了得到一些推文。我认为,通用使用是一种更容易绕过的方式。

+0

这不是一个答案。既然你提供了可能有用的建议,它应该只是OP下的评论。 – methai