Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to loop through this JSON Data using Jquery to display klout score?

API JSON Data:

{"status":200,"users":[{"twitter_screen_name":"mikerbrt","influencers":
[{"twitter_screen_name":"techcrunch","kscore":85.17},    
{"twitter_screen_name":"tinucherian","kscore":61.5},
{"twitter_screen_name":"scobleizer","kscore":84.08},
{"twitter_screen_name":"startwerk","kscore":42.12},
{"twitter_screen_name":"mikeschwede","kscore":61.89}]}]}

Script:

$( "#getTweets" ).bind( "click", function() {
    var twitterUsername = $( "#twitterUsername" ).val();   
     var url = "http://api.klout.com/1/soi/influenced_by.json?key=7zcwhzhdp4w45q9fdqw9gqsc" + "&users=" +  twitterUsername + "&callback=?";
    $.getJSON( url, function( data ) {
             for(var i=0; i < data.influencers.length; i++) {
  alert(data.influencers[i].kscore);
      } 
           });
});

HTML Data:

<div id="input">
    <span>Enter Twitter Username</span>
    <input id="twitterUsername" type="text" />
    <button id="getTweets">Get Tweets</button>
</div>
<ul></ul>

Required Output: Need to display klout score of influencers

like image 961
mikerbrt Avatar asked Jun 29 '11 11:06

mikerbrt


1 Answers

ok, I'm not sure if "Users" property will be treated as an array, or a Single Instance or users, but it will be either :

for(var i=0; i < data.users[0].influencers.length; i++) {
  alert(data.users[0].influencers[i].kscore);
} 

or

for(var i=0; i < data.users.influencers.length; i++) {
  alert(data.users.influencers[i].kscore);
} 

depending on how "users" is perceieved HTH. Dave

like image 79
Dave Long Avatar answered Oct 12 '22 19:10

Dave Long