Dustin Diaz has just posted JSON for the masses, an excellent article that will get JavaScript developers thinking about objects and how they can lead to organized, maintainable code. I just wanted to second his post, and say I’m on the JSON train as well. Go ahead and read Dustin’s post, and if you want to get on board I suggest trying the following steps to get yourself comfortably acquainted with JSON:

Become familiar with the official JSON in JavaScript page. Give it a good read, and bookmark it.

Using the JSON object example from the official page, try to send it to the server with JSON.stringify(). I use the code file given on the official page to do this.

Try to read the JSON string on the server, and convert it into a server side object. I use PHP, so JSON-PHP is my helper file of choice. Once you’ve got a PHP object, practice looping through it and accessing the data inside.

Using your PHP object, loop through the entire thing and learn how to write a JSON string both manually and using the JSON-PHP encode function. Then echo the results out, so that an Ajax call can pick up the response.

With an Ajax call, request the data from the PHP page and read it in using eval() or parse(). At this point, you will have a copy of the object from step 2. Learn how to loop through this object, and read the different values. Prototype Meets Ruby: A Look at Enumerable, Array and Hash was a must read for me at this step. If you’re not using Prototype, find your libraries equivalent, or write your own home grown solution.

And that should get you started. The steps above will lead to some frustration when they don’t work the first time. If you get through it, you will have a new tool that will become invaluable as you begin making more JavaScript heavy sites.

More Love For JSON by Ryan Campbell

This entry was posted
5 years ago
and was filed under Notebooks. Comments are currently closed.

· 7 Comments! ·

It’s great to read this. Interestingly, there’s currently a debate in the JSON Y! group about a new name for “AJAX” that drops the ‘X’ in favor of something more appropriate to JSON. My personal feeling is that this branding exercise is unnecessary - JSON is so self-evidently optimal for this stuff that it doesn’t need to be sold to anybody.