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
  • 354,903
  • 100
  • 647
  • 710
zjm1126
  • 34,604
  • 53
  • 121
  • 166

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
  • 795,719
  • 175
  • 1,089
  • 1,143
  • 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 – Ruan 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
  • 12,283
  • 25
  • 92
  • 144