Today I used JSON.
Been reading about it for a while, always intended to try it out, but never had the time or excuse. Well today I finally employed JSON to accomplish something. Before today, I had this uppity attitude that "anything JSON can do, XML does better". Well that's a lie. Now I see why JSON exists and needed to exist. What a useful invention!
In my experimental sandbox, I have a little AJAX calling a script that reads from my database. The server-side script outputs the data results as JSON, which was just as easy as outputting XML. Then over on the client side when the XmlHttpRequest is complete, I just take the JSON content and throw it into eval(). From then on, all my data is available in a convenient object. No parsing needed, no Xpaths or XSL. I think the decision to JSON instead of XML saved time, and will probably have a positive effect on performance, since the output is a little smaller than a comparable chunk of XML.
Do you AJAX? Then JSON is worth a try. The next time you're writing an internal web service to power an AJAXified app, consider using JSON instead of XML and see if you enjoy it.
Deciding to try JSON forced me to approach my challenge in a different way. Normally if I wanted to insert some asynchronous stuff on the page, I'd make an AJAX request, have my server script create some XML, use XSL to transform it, then receive that at the client and use Javascript innerHTML() to insert it into the DOM. With JSON, instead I got myself a native javascript object, then wrote a script that generated the DOM nodes using createElement(), createTextNode(), appendChild() using the JSONified data. Tomayto, Tomahto.
Filed under Uncategorized






February 11th, 2007 at 10:49 pm
And people think I am speaking a strange unknown language when we talk SEO… Nice post, mate. I just wish I could make some sense of it all :P