The Wayback Machine - https://web.archive.org/web/20080704134131/http://www.devx.com:80/webdev/Article/32651
advertisement
Premier Club Log In/Registration
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Download the sample news ticker application
Partners & Affiliates
advertisement
advertisement
Average Rating: 4/5 | Rate this item | 5 users have rated this item.
 Print Print
 
Speed Up Your AJAX-based Apps with JSON
Find out how to improve your Web application performance by leveraging AJAX and JSON. In particular, you'll see the advantages of using JSON over XML as a lightweight JavaScript data-interchange format. 

advertisement
SON stands for JavaScript Object Notation and is a lightweight data-interchange format. Because it is text-based it is easy for humans to read and write, and it has a regular syntax that's easy to parse programmatically. JSON is basically a subset of JavaScript and, as you'll see, is even easier to parse than XML.


AJAX stands for Asynchronous JavaScript and XML. It's a Web development technique used to make non-blocking calls to the server using JavaScript. The "and XML" part is likely to fade away as the time goes by. Even though AJAX initially used XML as the format for transferring data, any format will work, including HTML, plain text and—why not—JSON. After all, from a machine viewpoint XML, HTML and JSON are all plain text!

In this article you'll see how to develop a news ticker using AJAX, JSON, JavaScript and PHP. Figure 1 should clarify the intent of the application.

 
Figure 1. News Ticker in action: The image doesn't pay off since the scrolling feature is not visible.
If you don't know PHP and are more familiar with another server-side technology such as ASP.NET or J2EE, don't worry; the techniques apply to these technologies/languages as well, and you'll find it will be very easy to translate the PHP script into another language.

Furthermore, by developing the sample app, you'll see how to implement the Observer Pattern in JavaScript.

JSON vs. XML
JSON and XML are basically used for the same purpose—to represent and interchange data. I'll try to show you why you might want to use JSON rather than XML in an AJAX context by showing you an example of how an employee class (actually, a list of employees) might be represented, first in XML. and then in JSON. This side-by-side comparison should let you begin to understand how to represent data in JSON. Here's the XML version:

XML VersionJSON Version
   <?xml version='1.0' encoding='UTF-8'?>
   <employees>
      <employee>
         <surname>Lacava</surname>
         <name>Alessandro</name>
         <city>Milan</city>
         <state>Italy</state>
         <department>IT</department>
      </employee>
         
      <employee>
         <surname>Brown</surname>
         <name>John</name>
         <city>Rome</city>
         <state>Italy</state>
         <department>IT</department>
      </employee>
   </employees>

   {
      "employees" :
      [
        {
          "surname" : "Lacava",
          "name" : "Alessandro",
          "city" : "Milan",
          "state" : "Italy", 
          "department" : "IT"
        },
        {
          "surname" : "Brown",
          "name" : "John",
          "city" : "Rome",
          "state" : "Italy", 
          "department" : "IT"
        }
      ]
   }

As you can see JSON is less verbose than XML, which translates directly to improved performance—less data means fewer bytes to transfer and parse, in other words JSON requires less bandwidth and fewer memory and CPU resources. JavaScript developers know that JSON is a subset of JavaScript. Indeed, the JSON code you just saw is a literal representation of arrays and objects in JavaScript!

  Next Page: Arrays and Objects: the JSON Fundamental Pillars


Page 1: IntroductionPage 3: The Observer Pattern
Page 2: Arrays and Objects: the JSON Fundamental PillarsPage 4: Security Concerns and JSON Support in Other Languages
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Solutions
Whitepapers and eBooks
IBM eBook: Planning a Service Oriented Architecture
IBM eBook: Choosing the Right Architecture--What It Means for You and Your Business
Microsoft Article: Will Hyper-V Make VMware This Decade's Netscape?
Avaya Article: Using Intelligent Presence to Create Smarter Business Applications
Intel Go Parallel Article: Getting Started with TBB on Windows
Microsoft Article: 7.0, Microsoft's Lucky Version?
Avaya Article: How to Feed Data into the Avaya Event Processor
IBM Article: Developing a Software Policy for Your Organization
Microsoft Article: Managing Virtual Machines with Microsoft System Center
Intel Go Parallel Article: Intel Threading Tools and OpenMP
HP eBook: Storage Networking , Part 1
Microsoft Article: Solving Data Center Complexity with Microsoft System Center Configuration Manager 2007
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
Microsoft Silverlight Video: Creating Fading Controls with Expression Design and Expression Blend 2
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Silverlight 2 App and Walkthrough: Leverage Silverlight 2 with SQL Server and XML
IBM Article: Enterprise Search--Do You Know What's Out There?
HP Demo: StorageWorks EVA4400
Microsoft Article: The Progress and Promise of Deep Zoom
Microsoft How-to Article: Get Going with Silverlight and Windows Live
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES