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()
orparse()
. 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.
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.
@Ryan: Choo choo… Welcome aboard. I’ve actually been meaning to write about this for a while, so I’m glad you’re on the train with me. I hope others will see its usefulness in due time.
@Michael: I could see something more like AJASON. I’ll talk to the guys and see what we’re coming up with.
Dude, this’ll be awesome
It was definitely a great article by Dustin. For myself, Javascript seems so much more attractive an option with JSON.
> a new name for “AJAX†that drops the ‘X’ in > favor of something more appropriate to JSON
I sure hope it isn’t “AJAJ” cuz JAJ in Corean means penis…
Hello, Very good article. Javascript powered by JSON and AJAX is the solution to many problems with dynamic programming.
Thank you
Everyone needs a hug.