Created
June 19, 2013 15:32
-
-
Save influxweb/5815260 to your computer and use it in GitHub Desktop.
Parsing json with jQuery Parsing json data with jQuery is not complicated at all. Here is an efficient example on how to parse json data and append it to your web page. http://www.catswhocode.com/blog/useful-jquery-code-snippets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function parseJson(){ | |
//Start by calling the json object, I will be using a | |
//file from my own site for the tutorial | |
//Then we declare a callback function to process the data | |
$.getJSON('hcj.json',getPosts); | |
//The process function, I am going to get the title, | |
//url and excerpt for 5 latest posts | |
function getPosts(data){ | |
//Start a for loop with a limit of 5 | |
for(var i = 0; i < 5; i++){ | |
//Build a template string of | |
//the post title, url and excerpt | |
var strPost = '<h2>' | |
+ data.posts[i].title | |
+ '</h2>' | |
+ '<p>' | |
+ data.posts[i].excerpt | |
+ '</p>' | |
+ '<a href="' | |
+ data.posts[i].url | |
+ '" title="Read ' | |
+ data.posts[i].title | |
+ '">Read ></a>'; | |
//Append the body with the string | |
$('body').append(strPost); | |
} | |
} | |
} | |
//Fire off the function in your document ready | |
$(document).ready(function(){ | |
parseJson(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment