Skip to content

Instantly share code, notes, and snippets.

@OmarElgabry
Created January 20, 2015 13:26
Show Gist options
  • Select an option

  • Save OmarElgabry/db96f18ec92c8deff67c to your computer and use it in GitHub Desktop.

Select an option

Save OmarElgabry/db96f18ec92c8deff67c to your computer and use it in GitHub Desktop.
JSON Tutorial
{
"data": [
{
"id": "X12",
"from": {
"name": "John Kennedy", "id": "1458633"
},
"message": "JSON is much easier and better than XML",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/X999/posts/Y999"
},
{
"name": "Like",
"link": "http://www.facebook.com/X999/posts/Y999"
}
],
"type": "status",
"created_time": "2010-08-02T22:27:44+0000",
"updated_time": "2010-08-02T22:27:44+0000"
},
{
"id": "X45",
"from": {
"name": "Anna Smith", "id": "12587521"
},
"message": "On my way to write my very first JSON snippet",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/X998/posts/Y998"
},
{
"name": "Like",
"link": "http://www.facebook.com/X998/posts/Y998"
}
],
"type": "status",
"created_time": "2010-08-02T25:27:44+0000",
"updated_time": "2010-08-02T25:27:44+0000"
}
]
}
<!DOCTYPE html>
<html lang='en'>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>
JSON Tutorial
</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
function successFn(jsonData,status, xhr){
var facebookPosts = jsonData.data; // facebookPosts is an array, same as "data" in json file.
for (var i=0; i < facebookPosts.length; i++){
var id = facebookPosts[i].id;
var from = facebookPosts[i].from.name;
var msg = facebookPosts[i].message;
var type = facebookPosts[i].type;
var firstAction = facebookPosts[i].actions[0].name;
console.log(from+ " of ID: "+id+" wrote: "+msg+" as a "+type);
}
}
$.getJSON( "facebookData.json",successFn);
</script>
</head>
<body>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment