473,385 Members | 1,772 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

Benchmarking AJAX - JSON vs. XML

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 data class (actually, a list of PHP documentation pages) 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. The XML version:
XML Version JSON Version

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <root>
  3.     <data> 
  4.         <id>1</id>                    
  5.         <name>PHP </name> 
  6.     </data>
  7.     <data> 
  8.         <id>2</id>                    
  9.         <name>Table of Contents</name> 
  10.     </data>
  11.     <data> 
  12.         <id>7</id>                    
  13.         <name>Getting Started</name> 
  14.     </data>
  15.     <data> 
  16.         <id>8</id>                    
  17.         <name>Introduction</name> 
  18.     </data>
  19.     <data> 
  20.         <id>9</id>                    
  21.         <name>What is PHP?</name> 
  22.     </data>
  23.     <data> 
  24.         <id>10</id>                    
  25.         <name>What can PHP do?</name> 
  26.     </data>
  27.     <data> 
  28.         <id>11</id>                    
  29.         <name>A simple tutorial</name> 
  30.     </data>
  31.     <data> 
  32.         <id>12</id>                    
  33.         <name>What do I need?</name> 
  34.     </data>
  35. </root>



Expand|Select|Wrap|Line Numbers
  1.       {
  2.                 "data": [
  3.                            {
  4.                                 "id" :    "1",
  5.                                 "name" :    "PHP"                            
  6.                             },
  7.                             {
  8.                                 "id" :    "2",
  9.                                 "name" :    "Table of Contents" 
  10.                             },
  11.                             {
  12.                                 "id" :    "7",
  13.                                 "name" :    "Getting Started"  
  14.                             },
  15.                             {
  16.                                 "id" :    "8",
  17.                                 "name" :    "Introduction"
  18.                             },
  19.                             {
  20.                                 "id" :    "9",
  21.                                 "name" :    "What is PHP?"
  22.                             },
  23.                             {
  24.                                 "id" :    "10",
  25.                                 "name" :    "What can PHP do?"
  26.                             },
  27.                             {
  28.                                 "id" :    "11",
  29.                                 "name" :    "A simple tutorial"
  30.                             },
  31.                             {
  32.                                 "id" :    "12",
  33.                                 "name" :    "What do I need?" 
  34.                             }
  35.                     ]
  36.           };
  37.  
There's nothing complicated going on here; now data contains the JSON formatted data we've been looking at throughout this article. However, this doesn't do much, as the data still isn't in a format that is obviously useful.

JSON Responses + Accessing the data

While it might not be obvious, that lengthy string above is just an array, and once you've got that array in a JavaScript variable, you can access it easily. In fact, you can simply separate the array with period delimiters. So, to access the name of the first entry of the PHP documentation pages, you would use code like this in your javascript:

Expand|Select|Wrap|Line Numbers
  1. data.name[0];


Take note that the indexing is zero-based. So this begins with the data in the data variable; it then moves to the item called name and pulls off the first record ([0]); The result is the string value "PHP".

Here are a few more examples using the same variable.
Expand|Select|Wrap|Line Numbers
  1. data.name[1]
  2.   // Value is Table of Contentsy"
  3.  
  4.  
  5.  

With this little bit of syntax, you can work with any variety of JSON-formatted data, all without any extra JavaScript toolkits or APIs.

Modifying JSON data

Just as you can access data with the dot and bracket notation shown above, you can easily modify data in the same way:
Expand|Select|Wrap|Line Numbers
  1. data.name[1] = "PHP is HERE";


That's all you need to do to change data in a variable once you've converted from a string to JavaScript objects.

Test Now -Benchmarking AJAX - JSON vs XML


My test results here
Sep 24 '08 #1
0 5358

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: DartmanX | last post by:
Hi, Looking for recommendations for a decent API for AJAX work. I need it to be somewhat documented so I can figure out how to actually use it. My most critical need right now is clean code...
13
by: iannorton | last post by:
Hi there, I Recently started experimenting with AJAX and succesfully created an autocomplete box using a script that returned html from a serverside javascript page. I'm now at the point...
4
by: VK | last post by:
Google Trends is an all new service (started May 10) and I have not responsability for proper query or data accuracy. Overall seems pretty close to what could be observed by the post history in...
1
by: dan.goyette | last post by:
I'm fairly new to using AJAX. I'm currently developing a data grid application in coldfusion, using AJAX for paging/filter/sorting updates to the grid. So far I've just been returning raw html, and...
4
by: UKuser | last post by:
Hi, I'm working on the following code, which works fine in Firefox, but not in IE. The problem is its not posting the variable to my page and I'm thinking its something wrong with the...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
4
balajibirlasoft
by: balajibirlasoft | last post by:
Am using a default converter object in ajax to convert the tables collection to a JSON string. First Input for toJSON Method is strTempData(String Builder) Second Input is the object for which...
11
by: kj | last post by:
I would like to convert a form that currently uses the traditional CGI sequence (i.e. the action associated with the form sends a POST request to a server-side CGI script) to one that uses AJAX to...
3
by: George | last post by:
I am doing an AJAX call using JQuery on my page to which returns JSON objects and everything works fine. Now I decided to use ashx handler instead of and simply write JSON out. Then my problems...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.