1

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!!

5
  • 1
    JSONP is not magic. Your server needs to support JSONP. (unless you're on that domain) Commented Aug 15, 2013 at 16:00
  • 1
    What do you see in the console? Commented Aug 15, 2013 at 16:01
  • Use chrome dev tools and take a look at the network tab - what is send from the server to your client (your browser)? Commented Aug 15, 2013 at 16:02
  • if I use $("#posts").append("<li>"+data.news+"</li>"); with $.getJSON I get [object Object],[object Object] appended to posts Commented Aug 15, 2013 at 16:05
  • json.news[0].id will access that first id. Commented Aug 15, 2013 at 16:17

3 Answers 3

1

Inside your success function, loop over all of the elements in the news array:

for (var i=0; i < data.news.length; i++) {
    $('#posts').append('name: ' + data.news[i].id + '<br/>');
}
Sign up to request clarification or add additional context in comments.

1 Comment

I would recommend checking if any news exists: data.hasOwnProperty("news") prior to the loop or the news.length will error when news is undefined.
0

data.news is an array so you have to loop through it to get all the values

$(document).ready(function() {

    $.getJSON("http://s.example.com/myjson.php", function(data) {
        $.each(data.news, function(key, value) {
            $("#posts").append("<li>"+value.id+"</li>");
        });
    });


});

2 Comments

wow! thanks! this works great!! that's exactly what I was trying to do.
This will receive Cannot read property 'length' of undefined unless you check for news first (if none exist) with if (data.hasOwnProperty("news") $.each(...
0

In your JSON, news is an array.

You need to get the property from an element in the array:

data.news[42].id

You will need to figure out which element to get.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.