XML vs. JSON
I heard about JSON a while ago, but I never looked closely at it until this past week. I was looking for a way to communicate between JavaScript and a Servlet (or anything cgi-like) with an AJAX call. Normally I would construct an XML file, parse through and construct HTML chunks and use innerHTML to update a section of the page. The problem with this is, XML is kind of a pain in the butt for a lazy programmer, and why bother constructing an XML file if you’re going to have to parse it back to something else?
You can’t spell XMLHttpRequest without XML. But you can sure go without using it. I make a lot of AJAX calls on websites these days. I know it doesn’t degrade with browsers running without JavaScript enabled. That’s not my problem. I really don’t care for people who turn JavaScript off. JSON is a much better way to publish data into JavaScript, because JSON is written as a plain old JavaScript Object, which looks like a simple hash table.
There’s nothing for you to do, all you have to do is grab it from the server (which would show itself as a string in JavaScript) and run eval on it to turn it into an object. In Ruby on Rails, there’s a simple helper method called: .to_json which makes constructing a JSON object simple…but Rails makes everything simple, and you spend less time programming brainless things like parsing XML.
