John Livermore
John Livermore

Reputation: 31313

json2 stringify

I have seen several posts about json2.js and the stringify method it provides. However, the posts I have seen are from almost a year ago. Is there a better library to use today or does jQuery directly support stringify functionality?

Upvotes: 1

Views: 5494

Answers (2)

Anurag
Anurag

Reputation: 141869

The JSON object has been defined in ECMAScript 5th ed, and is already available in most modern browsers. No special setup is needed. Calling,

JSON.stringify(someObject)

will spit out a JSON representation of the passed in object. If you want compatibility for older browsers, then simply include Crocford's json2.js on your page. json2.js will use the browser's native implementation, if available.

Upvotes: 9

Kelly Sutton
Kelly Sutton

Reputation: 591

You can use the .serialize() method on a form to give you a JSON string of a jQuery form object. It's usually what I use if I'm doing an AJAX POST request.

Example:

<form id="SomeForm">
    <input name="hello" type="hidden" value="world" />
</form>

<script>
    $('#SomeForm').serialize(); // '{ "hello": "world" }'
</script>

Upvotes: 0

Related Questions