嘿伙计们正在使用facebook js sdk来检索发送的邮件数量。代码是
<script>
function statusChangeCallback(response) {
console.log('statusChangeCallback');
console.log(response);
if (response.status === 'connected') {
testAPI();
} else if (response.status === 'not_authorized') {
document.getElementById('status').innerHTML = 'Please log ' +
'into this app.';
} else {
document.getElementById('status').innerHTML = 'Please log ' +
'into Facebook.';
}
}
function checkLoginState() {
FB.getLoginStatus(function(response) {
statusChangeCallback(response);
});
}
window.fbAsyncInit = function() {
FB.init({
appId : '289533237896176',
cookie : true, // enable cookies to allow the server to access
// the session
xfbml : true, // parse social plugins on this page
version : 'v2.0' // use version 2.0
});
};
(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function testAPI() {
console.log('Welcome! Fetching your information.... ');
FB.api('/me/inbox?limit=50',function(response) { for (var i=0;i<response.data.length;i++) {
var thread = response.data[i];
for (var j=0;j<thread.comments.data.length;j++) {
var comment = thread.comments.data[j].message;
console.log(comment);
}
}
}
);
}
</script>
<fb:login-button scope="public_profile,email,read_mailbox" onlogin="checkLoginState();">
</fb:login-button>
<div id="status">
<div id="fb-root"></div>
</div>
这成功地检索了消息..但我需要的是找到发送的消息数量..我试过comment.length
但它没有帮助..我想找到发送的消息总数。< / p>
我的json对象是http://pastebin.com/dnc9U4zN
这可以用js sdk完成吗??希望你们能帮帮我..谢谢
答案 0 :(得分:0)
obj 是你的json对象:
var numberOfMessages = 0;
obj.data.forEach(function(value){
value.comments.data.forEach(function(value2){
numberOfMessages++;
});
});
console.log(numberOfMessages);