Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them, it only takes a minute:

This question already has an answer here:

Json data with this structure:

Title_day_hourminsec([
{"data":"x", "type":"y"},
{"data":"x", "type":"y"},
{"data":"x", "type":"y"},
{"data":"x", "type":"y"},
{"data":"x", "type":"y"},
{"data":"x", "type":"y"},
]);

from the call of jquery getJSON I'm not able to get the real content of the file, only got this is the html output:

[object Object]
[object Object]
[object Object]
[object Object]
[object Object]
[object Object]

this is the javascript snippet I've tried to get the json content:

<script type="text/javascript">

 $(document).ready(function(){
    $.getJSON("myremotedata",
      function(data){
      var items = [];

 $.each(data, function(key, val) {
items.push('<li id="' + key + '">' + val + '</li>');
});
 $('<ul/>', {
'class': 'my-new-list',
html: items.join('')
}).appendTo('body');
        });
  });


</script>

What am I missing?

share|improve this question

marked as duplicate by Felix Kling, Josh Mein, cale_b, Cole Johnson, uthark Sep 4 '13 at 18:30

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1  
The data shown is not valid JSON. Is it supposed to be JSONP? – nnnnnn Sep 4 '13 at 13:36
2  
You get an array of objects. [object Object] is the default string representation of an object. If you want to display specific properties, then you have to access them. – Felix Kling Sep 4 '13 at 13:37

2 Answers 2

up vote 6 down vote accepted

Try to modify your each loop like:

$.each(data, function (key, val) {
    items.push('<li id="' + key + '">' + val.data + ' ' + val.type + '</li>');
});

You are getting [object Object] since in the each loop, the variable val is actually an object. You need to use the Javascript dot object notation like val.data or val.type to get the values of the each object.

share|improve this answer
    
It work like a charm. Thanks a lot! – user2239318 Sep 4 '13 at 13:54
    
Glad it helped! – palaѕн Sep 4 '13 at 13:54

$.each() returns an index and the value at that index.

$.each(data, function(index, value) {
    // value contains {"data":"x", "type":"y"}
});

you can then use value.data or value.type for populating your list.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.