Check what holds in [object Object] in jQuery
I am using a jQuery plugin which returns the object, for example using the below code.
onSuccess: function开发者_运维技巧(data, status){
alert(data);
}
returns [object Object]
as it is returning the object i would like to know how do i check all the contents that holds inside that object using js alert();
also i would like to know if JS object and JSON are one and the same.
thank you
Use proper debugging tools like Firebug for Firefox or the built in Chrome developer tools. Then you can inspect objects with console.log
or console.dir
.
alert
is not meant for debugging. It can only output strings, which is of limited use as you already noticed.
also i would like to know if JS object and JSON are one and the same.
No, they are not. JSON is a data exchange format, similar to what XML can be used for, whereas a JavaScript object is a data type in JavaScript.
If you are wondering whether JavaScript object literals are JSON, then this answer has to be answered with no as well.
These are object literals
var foo = {foo: "bar"};
var foo = {"foo": "bar"};
whereas JSON can only exist inside strings in JavaScript:
var foo = '{"foo": "bar"}';
and which then has to be parsed into a equivalent JavaScript data type. This is done by parsers such as the built-in JSON
or json2.js.
Don't let their similar syntax/structure fool you.
For the first part of your question, check this: How to Loop through plain JavaScript object with objects as members?
And for your second question regarding json and js object: No, json is a string representation of a data structure, read here: http://en.wikipedia.org/wiki/JSON. However, it easy to parse a json to a js object.
Check this .data( key, value ) or .data( obj )
where
key - A string naming the piece of data to set.
value -The new data value; it can be any Javascript type including Array or Object.
obj - An object of key-value pairs of data to update.
OR
You can also use jQuery.each
精彩评论