I have a very basic json file that I am loading with jquery / javascript. Right now I am just trying to alert some of my data. The problem I am having is that I have 3 main items in my JSON file (called “category”), but for some reason you can only read the last one.
My JSON file looks like this:
{
"category":{
"name":"Caribbean Travel",
"article":{
"id": "1",
"title": "",
"teaser": "",
"image": "",
"date": "",
"map": "",
"body": ""
}
},
"category":{
"name":"European Travel",
"article":{
"id": "2",
"title": "",
"teaser": "",
"image": "",
"date": "",
"map": "",
"body": ""
}
},
"category":{
"name":"U.S. Travel",
"article":{
"id": "3",
"title": "",
"teaser": "",
"image": "",
"date": "",
"map": "",
"body": ""
}
}
}
I call up the json file and alert some information like so:
$(document).ready(function(){
$.getJSON("news.json",
function(data){
alert(JSON.stringify(data));
//alert(data.category.article.id);
});
});
So when I view the data, alls I see is the very last Category information (U.S. Travel). It doesn’t show the top 2 at all. And I can’t reference them using data.category[1] or anything like that.
Does anyone know what I am doing wrong here?
Thanks
Your json file should look like this:
data[0]will be the first category,data[1]the second one etc.