I copied this from the jquery site. I'm trying to learn how to get json data to a web page. The date has validated on: http://jsonlint.com/
The results from running the my script are: [object Object] [object Object] [object Object] [object Object] [object Object]
Do I need to parse the data or does getJSON do that.
This is the code I'm using:
<!doctype html>
<html>
<head>
<script type="text/javascript" src="js/jquery-1.7.1.js"></script>
<script type="text/javascript">
$.getJSON('data.json', 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>
</head>
<body>
</body>
</html>
Troubleshooting tips would be great and of course an answer would be great too!
data.json
[
{
"bytes": "476.577044"
},
{
"bytes": "136113.5289"
},
{
"bytes": "118870.8151"
},
{
"bytes": "55001.67852"
}
]
console.log(data) response:
[Object { bytes="476.577044"}, Object { bytes="136113.5289"}, Object { bytes="118870.8151"}, Object { bytes="55001.67852"}]
JSON.html (line 14)
[Object { bytes="476.577044"}, Object { bytes="136113.5289"}, Object { bytes="118870.8151"}, Object { bytes="55001.67852"}]
JSON.html (line 14)
[Object { bytes="476.577044"}, Object { bytes="136113.5289"}, Object { bytes="118870.8151"}, Object { bytes="55001.67852"}]
JSON.html (line 14)
[Object { bytes="476.577044"}, Object { bytes="136113.5289"}, Object { bytes="118870.8151"}, Object { bytes="55001.67852"}]