0

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?

2 Answers 2

1

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 of alert(result), you can try alert(JSON.stringify(result)).

Also, alert is ugly, annoying and hard to use; if you can, use console.log() and its friends instead, much easier on the programmer. Check the results in the JavaScript console. (This is under the assumption the alert() was for your own debugging benefit; if it's for users, try doing something in HTML instead.)

Sign up to request clarification or add additional context in comments.

3 Comments

sorry, I was actually alerting the 'items' variable, and it still returns [object Object]
@AnimeshTripathi: Same answer applies.
@AnimeshTripathi: Just to point out: this is a display problem, your data is fine. You don't need to convert your data to a string if you're using it normally.
0

It already is a variable, result is the json response that you can access the same way you would if you pushed it to items.

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.