28

Possible Duplicate:
Serializing to JSON in jQuery
Convert Object to JSON string

I want to convert array to json object in js or jquery. Is there any function is available or lib

Thanks in advance

Community
  • 1
  • 1
sachin
  • 1,015
  • 4
  • 11
  • 24

2 Answers2

28

In modern browsers you can simply use JSON.stringify(someJSObject) to convert any JavaScript object (that includes arrays) to a JSON string.

To ensure it also works in less modern browsers, include https://github.com/douglascrockford/JSON-js/blob/master/json2.js on your page.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
17

How about

var jsonString = JSON.stringify(myArray);
ladar
  • 5,858
  • 3
  • 26
  • 38
  • 2
    Great, if the user has a browser modern enough to support it. – Quentin Jan 12 '12 at 12:29
  • yes, your browser can't be 3 years old... If it is, it's not that difficult to create custom object->json parser – ladar Jan 12 '12 at 12:35
  • IE6 has about 1% share, so I don't really have to care about it. And I thought that IE8 has JSON support (with maybe some issues with callbacks). – ladar Jan 12 '12 at 13:08
  • I said IE 8 supports it. IE 7 doesn't. Between IE 7 and IE 8 you are talking about more than one in twenty users. (Usual provisos about browser stats apply). – Quentin Jan 12 '12 at 13:13
  • Well from "you need to hit IE 8 before it supports JSON" I understood that you mean IE8 doesn't support. Anyway, IE7 is 5 years old. I really don't see point in supporting such old browser (unless you developing some custom internal application for some company). Modern games don't run on old PCs, and web applications/pages are becoming similar. Anyway, let's not flame :) – ladar Jan 12 '12 at 13:29
  • 1
    "Hit" not "Pass". The point in support an old browser is that more than 1 in 20 people use it. That's a *lot* of customers / advert views / whatever. – Quentin Jan 12 '12 at 13:31