> > From: Joey Derrico > > > > I am a novice at AJAX and JSON (Ok, I am a novice at > > JavaScript.), and > > I am brand new to jQuery. I wanted to use JSON in a project I am > > working on and I read various tutorials on using JSON with jQuery > > however none of them answered some of my questions. The biggest one > > is, does jQuery support JSON or do I need to use another > > JSON library to use JSON with jQuery?
> From: Stephan Beal > > JSON is simply a data format. "Supporting" JSON simply means having: > > a) a function which can transform JS objects into a > JSON-compliant string. > b) a function which can transform a compliant string into a JS object. > > AFAIK, jQuery doesn't have any built-in support for JSON, but > it doesn't have to - it doesn't operate at a level where JSON > would be useful (except for possible selector-style traversal > of a JSON tree). > > The canonical JSON implementation for JavaScript is Doug > Crockford's json2.js, available here: > > http://www.json.org/ > > See JSON.stringify() and JSON.parse(). Actually, jQuery does provide quite a bit of built-in JSON support, and for a large category of apps, it provides all the support you need. If you want to make GET or POST requests to a server, get JSON data back, and use that data in your JavaScript code, you don't need json2.js. jQuery supports all of that, both for JSON data on your own server or JSONP data coming from other domains. Just use $.getJSON() or $.ajax() with the 'json' or 'jsonp' dataType as needed. If you want to *generate* JSON in your JavaScript code (e.g. if your server expects you to send it JSON data in a POST), JSON.stringify() from json2.js is the way to do it. If you want a "safer" JSON parser instead of the "eval-ing" JSON parser that jQuery uses, then JSON.parse() from json2.js will give you that. But for most apps you don't need this (and it's less useful than it might sound). Otherwise, you can just use the JSON support in jQuery. -Mike