views:

2431

answers:

3

I'm a Javascript newbie so bear with me. How can I serialise an object to JSON in Javascript? Is there a common external library I can use? I'm aware of jquery.getJSON(...) but this doesn't seem to quite do what I want, which is to simply get a string containing the JSON for an object in a piece of Javascript.

+3  A: 

Are you looking for something like the JSON.stringify()? it is what i use for my WCF calls.

http://www.json.org/js.html

Mike_G
That's what we use where I work.
R. Bemrose
Perfect, thanks.
Andy
And it has the added advantage of loking for native JSON implementations (such as that in upcoming ie8 and firefox 3.1), and not overriding them :D
olliej
A: 

Download http://www.JSON.org/json2.js, include it and do

json_data = JSON.stringify(obj);
Johannes Weiß
var json_data = JSON.stringify(obj);
Christian Sciberras
A: 

The thing is, http://www.JSON.org/json2.js doesn't serialize an object within an object, or is it a way to do it?

tymonn