How do I convert JavaScript objects to strings?
Example:
var o = {a:1, b:2} console.log(o) console.log('Item: ' + o)
Output:
Object {a = 1, b = 2} //Very good readable output:)
Item: [object Object object] //Don't know what's inside: (
#1st floor
To glance at jQuery-JSON Plug-in unit
Essentially, it uses JSON.stringify, but if the browser does not implement it, it falls back to its own parser.
#2nd floor
If you know the object is just a Boolean, date, string, number, and so on... the javascript String() function works well.I recently found this useful for handling values from the $.each function in jquery.
For example, the following converts all items in "value" to strings:
$.each(this, function (name, value) { alert(String(value)); });
More details here:
http://www.w3schools.com/jsref/jsref_string.asp
#3rd floor
Because firefox does not string some objects as screen objects; if you want the same result, for example, JSON.stringify(obj):
function objToString (obj) { var tabjson=[]; for (var p in obj) { if (obj.hasOwnProperty(p)) { tabjson.push('"'+p +'"'+ ':' + obj[p]); } } tabjson.push() return '{'+tabjson.join(',')+'}'; }
#4th floor
There is no solution for me here.JSON.stringify seems to be what many people say, but it cuts down on functions and looks bad for some of the objects and arrays I tried during the test.
I made my own solution, at least for Chrome.Publish here so anyone who finds this content on Google can find it.
//Make an object a string that evaluates to an equivalent object // Note that eval() seems tricky and sometimes you have to do // something like eval("a = " + yourString), then use the value // of a. // // Also this leaves extra commas after everything, but JavaScript // ignores them. function convertToText(obj) { //create an array that will later be joined into a string. var string = []; //is object // Both arrays and objects seem to return "object" // when typeof(obj) is applied to them. So instead // I am checking to see if they have the property // join, which normal objects don't have but // arrays do. if (typeof(obj) == "object" && (obj.join == undefined)) { string.push("{"); for (prop in obj) { string.push(prop, ": ", convertToText(obj[prop]), ","); }; string.push("}"); //is array } else if (typeof(obj) == "object" && !(obj.join == undefined)) { string.push("[") for(prop in obj) { string.push(convertToText(obj[prop]), ","); } string.push("]") //is function } else if (typeof(obj) == "function") { string.push(obj.toString()) //all other values can be done with JSON.stringify } else { string.push(JSON.stringify(obj)) } return string.join("") }
Edit: I know this code can be improved, but I have never done so.Improvement proposed by user Andrey Here With comments:
This is a slightly changed code that handles'null'and'undefined' without adding too many commas.
Use it at your own risk, as I have not verified it at all.As a comment, feel free to suggest any other improvements.
#5th floor
If you only output to the console, you can use console.log('string:', obj).Note the comma.