views:

40

answers:

1

How do i convert a json object to a json string?

var o = { name: "a", id:5};
var sz = //???
alert('The json will look like ' + sz);

-edit- i forgot i would like to do it in jQuery or naively if possible

+6  A: 

You are looking for the JSON.stringify method:

var sz = JSON.stringify(o);

This method is part of the ECMAScript 5 Standard, and almost every browser includes it.

For older browsers and IE < 8, you can include the json2.js file.

CMS