I’m trying to assign a JSON array from a URL to a variable. Here’s my code:
$.getJSON("tljson.json",function(result){
var items = [];
items.push(result);
});
However, ‘alerting’ the items only returns
[object Object],[object Object],[object Object],[object Object]
What am I doing wrong?
What you’re doing wrong is alerting the result. You have an array of four objects, but alert only shows the default text representation of objects,
[object Object]. Convert your data to string yourself before printing. For example, instead ofalert(result), you can tryalert(JSON.stringify(result)).Also,
alertis ugly, annoying and hard to use; if you can, useconsole.log()and its friends instead, much easier on the programmer. Check the results in the JavaScript console. (This is under the assumption thealert()was for your own debugging benefit; if it’s for users, try doing something in HTML instead.)