I'm new to JSON and have not been able to get the result I want. In learning, all I want to do is almost a HELLO WORLD for JSON, with the ultimate goal being to display the data in a table.
I have a JSON Call URL that gives me JSON data, and it's formatted correctly.
I have written a script to see if I can get an alert for my JSON data:
Updated
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script>
$.getJSON("http://li93-171.members.linode.com:8080/BrokerManager/getActiveBrokerNames/?callback=?", function(data){
console.log(data);
});
</script>
Alert shown says "[object Object],[object Object]" -- I'm obviously doing something wrong. Please help!
First of all, JSON stands for JavaScript Object Notation, which means when your JSON gets received by the client, jQuery will automatically change it from a normal string to a more usable Object. That is why all you see in the alert is [object Object] since alert can only display strings. All non-string values will be casted into strings.
alert(data);
alert(data.toString()); //both are the same
To display an Object for debugging usages, use console.log or
$("<pre>").html(JSON.stringify(data, null, 4)).appendTo("body");
http://jsfiddle.net/DerekL/4XayF/
don't use alert use console.log(data) with objects
if you are using chrome click control + shift + i to view your console , IE hit F12
Related
I have an AJAX call that get's a simple JSON back on success, writing the returned JSON into the variable named data.
The returned JSON has been generated within PHP through:
echo json_encode(array('message'=>0);.
Back in my javascript console I'm logging the output of data with console.log(data);. The output is {"message":0}, which seems correct.
However, if I try to log console.log(data.message); I am getting message: undefined.
What am I doing wrong in accessing this pretty basic JSON object?
I've been reading some articles about how to move through JSON object, e.g. https://www.tutorialspoint.com/json/json_overview.htm but I can't find my mistake.
Hopefully you can point me into the right direction :)
Thank you :)
You have to do JSON.parse(result) first to make it an object, because at the moment you're trying to access message property of a string.
You know there are 2 JSON functions in javascript, right...?
// returns string representation of the object
JSON.stringify(object);
// returns a new object built from the string
JSON.parse(object_stringified);
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 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.)
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.
I make an AJAX call to fetch some data, get the data back as a JSON Object (not a string).
When I log the object, I get the correct object and it's properties. However, when I try to log one of the objects properties, I get undefined.
For a screenshot of my code:
http://i.imgur.com/gnt3w.gif
For a screenshot of the console log:
http://i.imgur.com/DO09m.gif
What am I doing wrong?
It looks like your POST is returning data in an array, not as an individual object. I bet if you log data[0].bursary_name, you will see the correct logged output.
I am using JSON.parse to parse this JSON string
[{"created_at":"2012-01-24T22:36:21Z","name":"joe","age":42,"updated_at":"2012-01-24T22:36:21Z"}]
However I am simply getting this result as the output:
[object Object]
Which shouldn't be the result. I am using this within the Cappuccino framework. Does anyone know what I am doing wrong here?
[object Object] is what objects display when you call toString on them. It looks like you're taking your result and trying to call obj.toString()
Also, your JSON is an array with one element in it, so to verify that your result is correct, you can access the name property on the [0] index:
obj[0].name // should be "joe".
var text = '[{"created_at":"2012-01-24T22:36:21Z","name":"joe","age":42,"updated_at":"2012-01-24T22:36:21Z"}]';
var obj = JSON.parse(text);
alert(obj[0].name); //alerts joe
DEMO
Or get rid of the array, since it's not really doing much
var text = '{"created_at":"2012-01-24T22:36:21Z","name":"joe","age":42,"updated_at":"2012-01-24T22:36:21Z"}';
var obj = JSON.parse(text);
alert(obj.name); //still joe
DEMO
This is an array because it's in square brackets - [] - remove these and it should work...
Even though this is 'syntactically' correct the parser sees this as an array (which is a type of object) but won't do the work on it the way you'd expect.
Also for future reference:
Try to lint it, and see if your syntax is messed up: http://jsonlint.com/
This is an old subject, but nonetheless, I spent hours trying to figure out what was going on. So, hopefully this will help someone else in the future.
My issue was setting up a simple ajax call, and doing something with the resultset from said ajax call. However, no matter what I did, I couldn't get the json resultset to an object.
I ended up stepping through everything in the debugger window and noticed old code that was no longer active was showing up on the sidebar (dom detail). So, my data had been cached. I cleared the cache, and boom! Everything worked.
I searched for this topic, and I can't seem to find the right way to parse the JSON string to read the objects.
Here is my code
$.getJSON("<url>",
function(data) {
alert("success!"+data);
});
outputs:
success![object Object],[object Object],[object Object],[object Object]
Firebug shows correct response, and when I click JSON tab I see all the objects.
Thanks!
When a JSON string is parsed, it is turned into a Javascript object. If you use a string method on an object, the string [object Object] is returned.
You need to use object property access methods instead (e.g. alert(data.somekey);).
Don't use alert() for debugging in cases like this if you have Firebug available. Use console.log(data) and you will get direct insights into your JSON data.
In this case you'd have realized that there's absolutely nothing wrong :D .
JSON = JavaScript Object Notation precisely because it is the way to declare object literals in JavaScript. The data parameter is already a Javascript object (in your case an array of objects) that you can access as:
data[index].fieldname
enter your json string here, and click on the created tree view On the top left you will see how you can access it
link text