- 第1节:JSON 简介
- 第2节:JSON 语法
- 第3节:JSON 使用
- 第4节:JSONP 教程
- 第5节:JSON 数据类型
- 第6节:JSON 对象
- 第7节:JSON 模式
- 第8节:JSON 与 XML 对比
- 第9节:JSON 基础结构
- 第10节:JSON 数据格式
- 第11节:JSON 数据解析的方法
- 第12节:JSON 数据解析过程详解
- 第13节:JSON 解析URL参数
- 第14节:JSON 元素的添加删除
- 第15节:JSON 数据解析的3种方式
- 第16节:JavaScript 解析Json字符串的性能比较分析代码
- 第17节:JavaScript解析json格式数据简单示例
- 第18节:JSON数据遍历之for-in
- 第19节:jQuery 遍历Json的实现代码
- 第20节:JS遍历Json字符串
- 第21节:直接在JS里创建JSON数据然后遍历使用
- 第22节:JS循环遍历JSON数据的方法
- 第23节:js 遍历json返回的map内容示例代码
- 第24节:JSP+jquery使用ajax方式调用json的实现方法
- 第25节:jquery中的ajax方法怎样通过JSONP进行远程调用
- 第26节:jquery ajax jsonp跨域调用实例代码
- 第27节:jQuery插件jQuery-JSONP开发ajax调用使用注意事项
- 第28节:json的键名为数字时的调用方式
- 第29节:php跨域调用json的例子
- 第30节:jquery中ajax调用json数据的使用说明
- 第31节:ASP.NET(AJAX+JSON)实现对象调用
- 第32节:JavaScript转换与解析JSON方法实例详解
- 第33节:将JavaScript的jQuery库中表单转化为JSON对象的方法
- 第34节:javascript中JSON对象与JSON字符串相互转换实例
- 第35节:JavaScript中字符串(string)转json的方法
- 第36节:JavaScript将XML转成JSON的方法
- 第37节:java与javascript之间json格式数据互转介绍
- 第38节:转义字符(\)对JavaScript中JSON.parse的影响概述
- 第39节:JavaScript中json对象和string对象之间相互转化
- 第40节:javascript中将Object转换为String函数代码 (json str)
- 第41节:转换json格式的日期为Javascript对象的函数
- 第42节:AJAX跨域请求获取JSON数据
- 第43节:JS根据key值获取URL中的参数值
- 第44节:jquery用ajax方式从后台获取json数据后如何将内容填充到下拉列表
- 第45节:js获取json元素数量的方法
- 第46节:JSONP获取Twitter和Facebook文章数的具体步骤
- 第47节:js获取通过ajax返回的map型的JSONArray的方法
- 第48节:JSP中获取ExtJS.Ajax前台传递的JSON数据实现过程
- 第49节:jQuery中使用Ajax获取JSON格式数据示例代码
- 第50节:JQuery 获取json数据$.getJSON方法的实例代码
- 第51节:javascript中JSON对象与JSON字符串相互转换实例
- 第52节:JavaScript中字符串(string)转json的方法
- 第53节:IE6/IE7中JavaScript json提示缺少标识符、字符串或数字问题处理
- 第54节:使用JavaScript构建JSON格式字符串实现步骤
- 第55节:JavaScript 解析Json字符串的性能比较分析代码
- 第56节:javascript巧用eval函数组装表单输入项为json对象的方法
- 第57节:用javascript对一个json数组深度赋值示例
- 第58节:解析javascript 数组(以及json元素的添加删除)
- 第59节:js数组如何添加json数据
- 第60节:js/jquery解析json和数组格式的方法详解
JSONP获取Twitter和Facebook文章数的具体步骤
Twitter和Facebook要求身份验证来获取文章的数量,但事实证明你可以通过JSONP来获取这些信息,本文介绍如何使用一些简单的代码来获取并跳过验证这一步,请参考下面的步骤。
JavaScript代码
我将使用基本的JavaScript来告诉你如何做到这一点:
// 获取文章数量的封装对象
var socialGetter = (function() {
/* JSONP: 获取脚本的工具函数 */
function injectScript(url) {
var script = document.createElement('script');
script.async = true;
script.src = url;
document.body.appendChild(script);
}
return {
getFacebookCount: function(url, callbackName) {
injectScript('https://graph.facebook.com/?id=' + url + '&callback=' + callbackName);
},
getTwitterCount: function(url, callbackName) {
injectScript('http://urls.api.twitter.com/1/urls/count.json?url=' + url + '&callback=' + callbackName);
}
};
})();
// 回调方法
function twitterCallback(result) {
result.count && console.log('The count is: ', result.count);
}
function facebookCallback(result) {
result.shares && console.log('The count is: ', result.shares);
}
// 调用
socialGetter.getFacebookCount('http://davidwalsh.name/twitter-facebook-jsonp', 'facebookCallback');
socialGetter.getTwitterCount('http://davidwalsh.name/twitter-facebook-jsonp', 'twitterCallback');
因为有众多轻量级的 micro-frameworks来处理JSONP,所以本文最重要的部分可能是获取信息的url了。根据需要和习惯选择你的JSONP工具!
Twitter和Facebook对于这些请求肯定有数量和频率上的限制,所以如果你的网站访问量很大,则JSONP很可能会被拦截或屏蔽。一种快速的解决方案是将文章数量信息存储在sessionStorage中,但这只是针对单个用户的方式。如果你运行的网站流量较大,你最好选择在服务器端抓取数据并缓存下来,并在一定的时间内自动刷新。
国外社交网站获取分享数量APIs
GET URL:
http://cdn.api.twitter.com/1/urls/count.JSon?url=http://stylehatch.co
返回结果:
{
"count":528,
"url":"http://stylehatch.co/"
}
GET URL:
http://graph.facebook.com/?id=http://stylehatch.co
返回结果:
{
"id": "http://stylehatch.co",
"shares": 61
}
GET URL:
http://api.pinterest.com/v1/urls/count.json?callback=&url=http://stylehatch.co
返回结果:
({"count": 0, "url": "http://stylehatch.co"})
GET URL:
http://www.linkedin.com/countserv/count/share?url=http://stylehatch.co&format=json
返回结果:
{
"count":17,
"fCnt":"17",
"fCntPlusOne":"18",
"url":"http:\/\/stylehatch.co"
}
Google Plus
POST URL:
https://clients6.google.com/rpc?key=YOUR_API_KEY
POST body:
[{
"method":"pos.plusones.get",
"id":"p",
"params":{
"nolog":true,
"id":"http://stylehatch.co/",
"source":"widget",
"userId":"@viewer",
"groupId":"@self"
},
"jsonrpc":"2.0",
"key":"p",
"apiVersion":"v1"
}]
返回结果:
[{
"result": {
"kind": "pos#plusones",
"id": "http://stylehatch.co/",
"isSetByViewer": false,
"metadata": {
"type": "URL",
"globalCounts": {
"count": 3097.0
}
}
} ,
"id": "p"
}]
StumbledUpon
GET URL:
http://www.stumbleupon.com/services/1.01/badge.getinfo?url=http://stylehatch.co
返回结果:
{
"result":{
"url":"http:\/\/stylehatch.co\/",
"in_index":true,
"publicid":"1iOLcK",
"views":39,
"title":"Style Hatch - Hand Crafted Digital Goods",
"thumbnail":"http:\/\/cdn.stumble-upon.com\/mthumb\/941\/72725941.jpg",
"thumbnail_b":"http:\/\/cdn.stumble-upon.com\/bthumb\/941\/72725941.jpg",
"submit_link":"http:\/\/www.stumbleupon.com\/submit\/?url=http:\/\/stylehatch.co\/",
"badge_link":"http:\/\/www.stumbleupon.com\/badge\/?url=http:\/\/stylehatch.co\/",
"info_link":"http:\/\/www.stumbleupon.com\/url\/stylehatch.co\/"
},
"timestamp":1336520555,
"success":true
}
这里有一个网站封装了一个小插件,专门用来在页面上显示社交网站分享工具条,可以直接拿过来用,比较方便!http://sharrre.com/
Facebook,Twitter,LinkedIn比较常用,给出调用API的例子:
// Facebook
$.getJSON("http://graph.facebook.com/?id=http://stylehatch.co", function (d) {
$("#fackbook_count").text("The Facebook Share count is: " + d.shares);
});
// Twitter
$.getJSON("http://cdn.api.twitter.com/1/urls/count.json?url=http://stylehatch.co&callback=?", function (d) {
$("#twitter_count").text("The Twitter Share count is: " + d.count);
});
// LinkedIn
$.getJSON("http://www.linkedin.com/countserv/count/share?url=http://stylehatch.co&callback=?", function (d) {
$("#linkedin_count").text("The LinkdeIn Share count is: " + d.count);
});
IE浏览器可能会无法正确获取到Facebook返回的数据,可以尝试在URL后面加上&callback=?,这样JQuery会将它当成JSONP来调用。
Facebook还有另一个API返回XML数据,调用方法是这样的:
$.get("http://api.facebook.com/method/links.getStats?urls=http://stylehatch.co", function (d) {
$("#fackbook_count").text("The Facebook Share count is: " + $(d).find("total_count").text());
});
如果在IE浏览器下出现No Transport错误而无法获取到Facebook返回的数据,尝试在JavaScript代码的最前面加上$.support.cors = true;允许跨域访问数据。
将代码进行封装
我们将上面Facebook,Twitter,LinkedIn三个社交网站的API进行封装,以方便页面调用。
$.fn.getShareCount = function (url) {
var self = this;
var displayShareCount = function (val, obj) {
if (!isNaN(val) && val > 0) {
obj.show();
if (val > 999) {
obj.attr("title", val);
obj.text("500+");
}
else
obj.text(val);
}
};
return {
facebook: function () {
$.get("http://api.facebook.com/method/links.getStats?urls=" + url, function (d) {
var c = $(d).find("total_count").text();
self.each(function () { displayShareCount(c, $(this)); });
});
},
twitter: function () {
$.getJSON("http://cdn.api.twitter.com/1/urls/count.json?url=" + url + "&callback=?", function (d) {
self.each(function () { displayShareCount(d.count, $(this)); });
});
},
linkedin: function () {
$.getJSON("http://www.linkedin.com/countserv/count/share?url=" + url + "&callback=?", function (d) {
self.each(function () { displayShareCount(d.count, $(this)); });
});
}
};
};
然后在页面上这样调用:
$(function () {
var shareUrl = window.location.href.toLowerCase();
$('#fackbook_count').getShareCount(shareUrl).facebook();
$('#twitter_count').getShareCount(shareUrl).twitter();
$('#linkedin_count').getShareCount(shareUrl).linkedin();
});