Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

json jsonp

  • Loading branch information...
commit 651057c63814888fedf8444470cc1270f50f72ce 1 parent ee61b79
@angus-c authored
View
26 src/json_jsonp/01.json_example
@@ -0,0 +1,26 @@
+{
+ "cookies": {
+ "oatmeal": {
+ "ingredients": [
+ "flour",
+ "sugar",
+ "oats",
+ "butter"
+ ],
+ "calories": 430,
+ "eatBy": "2010-12-05",
+ "kosher": true
+ },
+ "chocolate": {
+ "ingredients": [
+ "flour",
+ "sugar",
+ "butter",
+ "chocolate"
+ ],
+ "calories": 510,
+ "eatBy": "2010-12-03",
+ "kosher": true
+ }
+ }
+}
View
20 src/json_jsonp/02.xml_example
@@ -0,0 +1,20 @@
+<cookies>
+ <oatmeal>
+ <ingredients>flour</ingredients>
+ <ingredients>sugar</ingredients>
+ <ingredients>oats</ingredients>
+ <ingredients>butter</ingredients>
+ <calories>430</calories>
+ <eatBy>2010-12-05</eatBy>
+ <kosher>true</kosher>
+ </oatmeal>
+ <chocolate>
+ <ingredients>flour</ingredients>
+ <ingredients>sugar</ingredients>
+ <ingredients>butter</ingredients>
+ <ingredients>chocolate</ingredients>
+ <calories>510</calories>
+ <eatBy>2010-12-03</eatBy>
+ <kosher>true</kosher>
+ </chocolate>
+</cookies>
View
2  src/json_jsonp/03.eval_json
@@ -0,0 +1,2 @@
+var myCookies = eval('(' + cookieJSON + ')');
+myCookies.cookies.chocolate.ingredients[1]; //"sugar"
View
12 src/json_jsonp/04.json_parse
@@ -0,0 +1,12 @@
+function dateReviver(key, value) {
+ if (typeof value === 'string') {
+ var a = /^(\d{4})-(\d{2})-(\d{2})$/.exec(value);
+ if (a) {
+ return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3]));
+ }
+ }
+ return value;
+};
+
+var myCookies = JSON.parse(cookieJSON, dateReviver);
+myCookies.cookies.oatmeal.eatBy; //Sat Dec 04 2010 16:00:00 GMT-0800 (Pacific Standard Time)
View
13 src/json_jsonp/05.json_stringify
@@ -0,0 +1,13 @@
+JSON.stringify(cookies, ['cookies','oatmeal','chocolate','calories'], '\t')
+/*
+'{
+ "cookies":{
+ "oatmeal":{
+ "calories":430
+ },
+ "chocolate":{
+ "calories":510
+ }
+ }
+}'
+*/
View
4 src/json_jsonp/06.script_tag
@@ -0,0 +1,4 @@
+var scriptTag = document.createElement('SCRIPT');
+scriptTag.src = "http://www.twitter.com/status/user_timeline/angustweets.json?count=5";
+
+document.getElementsByTagName('HEAD')[0].appendChild(scriptTag);
View
11 src/json_jsonp/07.jsonp_example
@@ -0,0 +1,11 @@
+var logIt = function(data) {
+ //print last tweet text
+ window.console && console.log(data[0].text);
+}
+
+var scriptTag = document.createElement('SCRIPT');
+scriptTag.src = "http://www.twitter.com/status/user_timeline/angustweets.json?count=5&callback=logIt";
+
+document.getElementsByTagName('HEAD')[0].appendChild(scriptTag);
+/* console will log:
+@marijnjh actually I like his paren-free proposal (but replacing global w/ modules seems iffy) JS needs to re-assert simplicity as an asset */
View
32 src/json_jsonp/08.jsonp_util
@@ -0,0 +1,32 @@
+var jsonp = {
+ callbackCounter: 0,
+
+ fetch: function(url, callback) {
+ var fn = 'JSONPCallback_' + this.callbackCounter++;
+ window[fn] = this.evalJSONP(callback);
+ url = url.replace('=JSONPCallback', '=' + fn);
+
+ var scriptTag = document.createElement('SCRIPT');
+ scriptTag.src = url;
+ document.getElementsByTagName('HEAD')[0].appendChild(scriptTag);
+ },
+
+ evalJSONP: function(callback) {
+ return function(data) {
+ var validJSON = false;
+ if (typeof data == "string") {
+ try {validJSON = JSON.parse(data);} catch (e) {
+ /*invalid JSON*/}
+ } else {
+ validJSON = JSON.parse(JSON.stringify(data));
+ window.console && console.warn(
+ 'response data was not a JSON string');
+ }
+ if (validJSON) {
+ callback(validJSON);
+ } else {
+ throw("JSONP call returned invalid or empty JSON");
+ }
+ }
+ }
+}
View
15 src/json_jsonp/09.jsonp_util_usage
@@ -0,0 +1,15 @@
+//The U.S. President's latest tweet...
+var obamaTweets = "http://www.twitter.com/status/user_timeline/BARACKOBAMA.json?count=5&callback=JSONPCallback";
+jsonp.fetch(obamaTweets, function(data) {console.log(data[0].text)});
+
+/* console logs:
+From the Obama family to yours, have a very happy Thanksgiving. http://OFA.BO/W2KMjJ
+*/
+
+//The latest reddit...
+var reddits = "http://www.reddit.com/.json?limit=1&jsonp=JSONPCallback";
+jsonp.fetch(reddits , function(data) {console.log(data.data.children[0].data.title)});
+
+/* console logs:
+You may remember my kitten Swarley wearing a tie. Well, he's all grown up now, but he's still all business. (imgur.com)
+*/
Please sign in to comment.
Something went wrong with that request. Please try again.