Convert object's source to string?

Y

Yansky

Is there a way to convert an object to it's source code as a string
without using the "toSource()" method? What I want to do is something
like this:

var myJSONObject = {"bindings": [
{"ircEvent": "PRIVMSG", "method": "newURI", "regex":
"^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex":
"^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex":
"^random.*"}
]
};

convertToString(myJSONObject);

which would ooutput: '{bindings:[{ircEvent:"PRIVMSG", method:"newURI",
regex:"^http://.*"}, {ircEvent:"PRIVMSG", method:"deleteURI",
regex:"^delete.*"}, {ircEvent:"PRIVMSG", method:"randomURI",
regex:"^random.*"}]}'

Any help would be appreciated.
Cheers,
Yansky.
 
M

Martin Honnen

Yansky said:
Is there a way to convert an object to it's source code as a string
without using the "toSource()" method? What I want to do is something
like this:

var myJSONObject = {"bindings": [
{"ircEvent": "PRIVMSG", "method": "newURI", "regex":
"^http://.*"},
{"ircEvent": "PRIVMSG", "method": "deleteURI", "regex":
"^delete.*"},
{"ircEvent": "PRIVMSG", "method": "randomURI", "regex":
"^random.*"}
]
};

convertToString(myJSONObject);

which would ooutput: '{bindings:[{ircEvent:"PRIVMSG", method:"newURI",
regex:"^http://.*"}, {ircEvent:"PRIVMSG", method:"deleteURI",
regex:"^delete.*"}, {ircEvent:"PRIVMSG", method:"randomURI",
regex:"^random.*"}]}'

You can try

JSON.stringify(myJSONObject)

where supported natively (e.g. IE 8, FF 3.5) or you need to grab
http://www.json.org/json2.js, then you have JSON.stringify even where
not supported by the script engine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,585
Members
45,080
Latest member
mikkipirss

Latest Threads

Top