I have a JSON file with dummy articles in it for testing purposes. I'm trying to extract the information from the JSON file using jQuery to create an array of objects using the pulled information.
Here's my JSON file:
{
"news": [
{
"title": "Test 1",
"author": "Chicago Tribune",
"source": "http://www.chicagotribune.com/",
"preview": "Long-simmering . . .",
"picture": "IMG"
},
{
"title": "Test 2",
"author": "New York Times",
"source": "http://www.nytimes.com/",
"preview": "Information . . .",
"picture": "IMG"
},
{
"title": "Test 3",
"author": "Chicago Tribune",
"source": "http://www.chicagotribune.com/",
"preview": "Blah blah blah . . .",
"picture": "IMG"
}
]
}
Here's my Javascript file:
// Article array
var articles = [];
// Article 'class'
function Article (title, author, preview, picture) {
this.title = title;
this.author = author;
this.preview = preview;
this.picture = picture
}
// Pull data from JSON file
$.getJSON('data.json', function(data) {
$.each(data.news, function (i, j) {
articles.push(new Article(j.title, j.author, j.preview, j.picture));
});
});
for (i = 0; i < articles.length; i++) {
Console.log('Article ' + i + ": " + articles[i].title);
}
The articles
array is not filling up and there doesn't seem to be any objects created. My next step would be iterating through each file in the array and posting that data to a page using the newly-filled array of articles.