Thursday, September 10, 2009

Video tutorials - JSON in Ajax(PHP and JavaScript)

Json library - http://www.json.org/json2.js .

Include above mentioned library before coding json code any further. Even though you can get same kind of functionality without using this library with pure javascript .

[obj].toSource() === JSON.stringify([obj]);
eval([obj].toSource()) === [obj];
// [obj] == any javascript object.

Remember don't include any methods/functions in your javasscript object if you are gonna pass its JSON around different languages. [obj].toSource() will also enclose the object's source (JSON) with parranthesis '([obj_source])', which is a necessity for javascript's eval() method. While this apporch may cause problem when you deal with JSON in different languages, like un quoted 'names' and stuff. so better off use the standard JSON library provided at the top.

PHP 5.x and above have built in support for encoding and decoding JSON, for previous versions download a JSON package from PHP pear or use a JSON library which is included in Major PHP frameworks.


No comments: