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.

Matt Ball
  • 344,413
  • 96
  • 627
  • 693
zjm1126
  • 32,782
  • 53
  • 118
  • 163

2 Answers2

4

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
  • 756,363
  • 169
  • 1,062
  • 1,111
  • can JSON object easy to parse an json string to an object ? – zjm1126 Aug 24 '10 at 21:27
  • 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 Aug 24 '10 at 21:28
  • @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 Aug 24 '10 at 21:41
0

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

Doug Molineux
  • 11,955
  • 25
  • 89
  • 141