API JSON数据:
{"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}]}]}
脚本:
$( "#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数据:
<div id="input">
<span>Enter Twitter Username</span>
<input id="twitterUsername" type="text" />
<button id="getTweets">Get Tweets</button>
</div>
<ul></ul>
必需输出:需要显示影响者的klout分数
答案 0 :(得分:3)
好的,我不确定“Users”属性是被视为数组,还是单个实例或用户,但它将是:
for(var i=0; i < data.users[0].influencers.length; i++) {
alert(data.users[0].influencers[i].kscore);
}
或
for(var i=0; i < data.users.influencers.length; i++) {
alert(data.users.influencers[i].kscore);
}
取决于“用户”的感知方式 HTH。 戴夫
答案 1 :(得分:2)
您未选择users
中的data.users.influencers
:
$.getJSON( url, function( data ) {
$.each(data.users,function(u,users){
$.each(users.influencers,function(i,user){
alert(user.kscore);
});
});
}
答案 2 :(得分:0)
因为您只获得一个用户的数据,所以您可以这样做:
<div id="input">
<span>Enter Twitter Username</span>
<input id="twitterUsername" type="text" />
<button id="getTweets">Get Tweets</button>
</div>
<ul id="klout"></ul>
$( "#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 ) {
$.each(data.users[0].influencers,function(j, influenser) { // array
$("#klout").append('<li>'+influenser.twitter_screen_name+':'+influenser.kscore+'</li>');
});
});
});