I just started learning json using jquery and I'm having some difficulties, I've spent two days, I've followed lots of tutorial both on youtube and blogs but what they show isn't what I have in my case. Their json is different from mine. They got multiple keys I've only one "news"
the JSON response from the server I get is like this:
{
"news":[
{
"id":48927,
"name": "The title goes right here",
"url": "www.example.com",
"date":"16 August 2013",
"image":"img.example.com\/image.jpg"
},
{
"id": 48908,
"name":"The title goes right here — photography",
"url":"www.example.net",
"date":"17 August 2013",
"image":"img.example.net\/image2.jpg"
}
]
}
in reality what I get from the server is more like this:
{"news":[{"id":48927,"name": "The title goes right here","url": "www.example.com","date":"16 August 2013","image":"img.example.com\/image.jpg"},{"id": 48908,"name":"The title goes right here — photography","url":"www.example.net","date":"17 August 2013","image":"img.example.net\/image2.jpg"}]}
but they both valid! I've already check with JSON LINT.
so what I'm not able to do is to get all the info from JSON and append them in my #posts
I've tried both $.getJSON & $.ajax but I get undefined as response
$(document).ready(function() {
$.ajax({
url: 'http://s.example.com/myjson.php',
data: 'GET',
dataType: 'json',
success: function(data){
$('#posts').append('name: ' + data.news.id + '<br/>');
},
});
});
in code above I've tried in append also with data, data.id, data.news, data.url etc. but alway getting undefined or null.
$(document).ready(function() {
$.getJSON("http://s.example.com/myjson.php", function(data) {
$.each(data, function(key, value) {
$("#posts").append("<li>"+data.news+"</li>");
});
});
});
same as $.ajax tried different things in append but nothing.
what I want to do is to have all the data id, name, url, date, image appended for everyone of the "things" I've in my json file. But unfortunately I'm stuck at the beginning. It's really frustrating, because it seems so simply but I can't figure out a way to do it. Please help me!!
$("#posts").append("<li>"+data.news+"</li>");with$.getJSONI get[object Object],[object Object]appended to postsjson.news[0].idwill access that first id.