mcbeav
mcbeav

Reputation: 12275

Help with jQuery grabbing json data

I am having some trouble here, my json data is outputting as follows:

{"date":[{"day_w":"Tuesday","day_n":"28","month":"Dec"}],"subscriptions":[{"subscribe":"example1"},{"subscribe":"example2"},{"subscribe":"example3"}]}

I am using the jQuery code:

$.getJSON("example.php",function(data){

$.each(data.subscriptions, function(i, item) {
    var subscribeData = "<li>"+ item.subscribe +"</li>";
    $('#list').append(subscribeData);
});

but I am having an issue grabbing the date array. I don't want to have to use .each because there is only one array holding the date. Does this make sense? Can anyone please help?

Upvotes: 1

Views: 133

Answers (3)

Hari Pachuveetil
Hari Pachuveetil

Reputation: 10374

Try this - http://jsfiddle.net/FloydPink/bAtEW/

Upvotes: 1

Samir Talwar
Samir Talwar

Reputation: 14330

Why is date an array at all? Why not just have the object in there directly?

{"date":{"day_w":"Tuesday","day_n":"28","month":"Dec"},"subscriptions":[...

If that's not an option, you can just access date[0]:

doSomethingWith(data.date[0].day_w);

Upvotes: 1

SLaks
SLaks

Reputation: 887453

You can write data.date[0] to get the first object in the array.

Upvotes: 1

Related Questions