1

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?

1
  • Are you going to use the date value within the $.each() loop? Commented May 12, 2011 at 16:28

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

its returning [object Object]
That's because it is an object. You probably want one of the object's properties.
1

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

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.