views:

51

answers:

2

I know to use eval() to parse JSON to an object, but how do I parse an object to JSON format using JavaScript?

Thanks.

A: 

Usually if you have a json and want to access something underneath it you just put a dot, like if the name of your json is msg and you want the location parameter it would be:

msg.location

Pete Herbert Penito
+4  A: 

The newer browsers support JSON.stringify. You can also download and include it yourself.

var json = JSON.stringify(yourObject);

Afaik jQuery does not provide such a method.

Felix Kling
can JSON object easy to parse an json string to an object ?
zjm1126
It's not a simple task and I don't think jquery provides this. JSON is not supported in IE. http://www.json.org/json2.js will do what you need with JSON.stringify and JSON.parse. Ext-core has methods called Ext.encode and Ext decode which do the same thing http://www.sencha.com/products/core/docs/?class=Ext
Juan Mendes
@zjm1126: It don't know exactly what you mean, but `JSON` also provides the `JSON.parse` method. It is described on the site I linked to.
Felix Kling