aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Head Rush Ajax and JSON Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Head Rush Ajax and JSON" Watch "Head Rush Ajax and JSON" New topic
Author

Head Rush Ajax and JSON

Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

There is a whole chapter on JSON and XML.
I'd like to ask how popular JSON is, and how far did you explain it in the book.
Thank you.


[My Blog]
All roads lead to JavaRanch
Brett McLaughlin
author
Ranch Hand

Joined: Sep 01, 2004
Posts: 30
I have mixed feelings about JSON, to be honest. I don't particularly love it as a data format for Ajax, because I don't feel it's that readable. It's arguably less verbose -- and therefore, also in theory, faster to send across the network -- than XML, but marginally so. And I don't think it's very intuitive.
That said, there are a ton of people who really like JSON, and I'm not religious about what data format you use, so it's covered. In fact, I really treat it in the book as a competitor to XML (they have a boxing match, so to speak), and compare and contrast them heavily.
In terms of depth of coverage, I think you'll find that once you get the basics on how JSON looks and is formatted (which is covered), it's a piece of cake. So I think most people looking for anything but the most extreme, advanced, unusual use-cases for JSON will find everything they need for JSON in the book.
But then again, I'm biased :-)

Thanks
Brett


Series Editor, Head First<br />Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0596102259/newinstance-20" target="_blank" rel="nofollow">Head Rush Ajax</a> and <a href="http://www.amazon.com/Head-First-Object-Oriented-Analysis-Design/dp/0596008678/ref=pd_bbs_sr_1/104-5348268-5670331?ie=UTF8&s=books&qid=1192568453&sr=8-1/newInstance-20" target="_blank" rel="nofollow">Head First OOA&D</a>
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Thank you
Linda Walters
Ranch Hand

Joined: Apr 30, 2005
Posts: 100
Brett, if I may, I'll add my $0.02 on the JSON vs. XML title bout.
I think the place where JSON is the most useful is if your JavaScript application is creating a lot of objects to hold the application's data. Sending the data in a format that is essentially already JavaScript would save much parsing of XML.

I think the down-side is not on the browser with its JavaScript, but on the server-side, especially if, like our enterprise application, the web-tier communicates with our back-end server, which does most of the "heavy lifting" even for our Eclipse Rich Client desktop client and that server in turn retrieves data from any one of several RDBMS servers. That is just way too much heavy XML zapping back and forth to expect to change it all into JSON just for the web-based client, when we can use the exact same XML as we would send to the desktop app.

JSON is interesting and I may end up using it a lot on my own web site and some of the sites that I do pro bono for local non-profits, but I think JSON's utility in the enterprise stack is quite limited.


<a href="http://labryssystems.net/pblog/index.php" target="_blank" rel="nofollow">Javaville Gazette</a><br />Non-cooperation with evil is a duty. -- Mahatma Gandhi
Weerawit Maneepongsawat
Ranch Hand

Joined: Apr 11, 2002
Posts: 203
it has a lot discussion on ajax response, pls check here



Weerawit<br />SCJP 1.4, SCWCD 1.2, SCWCD 1.4, SCBCD 1.3, SCBCD 5.0, SCMAD 1.0, SCJA 1.0, SCDJWS, SCJD, SCEA, SCDJWS 1.5
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

I have mixed feelings about JSON, to be honest. I don't particularly love it as a data format for Ajax, because I don't feel it's that readable. It's arguably less verbose -- and therefore, also in theory, faster to send across the network -- than XML, but marginally so. And I don't think it's very intuitive.


Hello Brett,
Nice to have you here in JR. Question - why is readability important becasue we extract the data and display it on the screen?


Groovy
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

How does the server create the JSON response object? Some AJAX framework sends the response ?
Liyaquat Ali
Ranch Hand

Joined: Nov 16, 2005
Posts: 156
Ajax is used for handling the response from the server.

If the server response header has a content-type of "text/plain" you have to use the responseText property to retrieve the response data.

If the server response header has a content-type of "text/xml" then the XMLHttpRequest parsed XML data available through responseXML property.

If the server side service(or jsp/asp/php/cgi etc) sends a JSON response it will be in the JSON format which will have a content-type response header of "text/plain", hence it will need to be retrieved using the responseText property.

Parsing the JSON response is much easier then parsing the XML response, thats one difference between then two reponses.

Regarding sending the JSON response from the server side, there are frameworks available which can create JSON from data which can be then readily sent to the response.

I hope this helps.


Excuse me while I kiss the sky (Jimi Hendrix)
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Originally posted by Pradip bp:
How does the server create the JSON response object? Some AJAX framework sends the response ?


The very basic idea behind it is:

You have to create the JavaScript code on the server. Just like how you would create any dynamic string. Loop through your results and build the object.

When you get the responseText back, it is evaluated with eval method and you have the JavaScript object.

Just search Google for JSON tutorial and you can find a lot of information.

There is a project called BadgerFish that does the translation from XML to JSON for you. See this: http://ajaxian.com/archives/badgerfish-translating-xml-to-json

Eric
 
jQuery in Action, 2nd edition
 
subject: Head Rush Ajax and JSON