如何使用 Jquery 循环此 JSON 数据以显示 klout 分数?
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 分数
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
好的,我不确定“Users”属性是否会被视为数组、单个实例或用户,但它将是:
或
取决于“用户”的感知方式
HTH。
戴夫
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 :
or
depending on how "users" is perceieved
HTH.
Dave
您没有选择
data.users.influencers
中的users
:You aren't selecting the
users
indata.users.influencers
:由于您只能获取一个用户的数据,因此您可以执行以下操作:
DEMO HERE
since you only get ONE user's data, you can do this:
DEMO HERE