Section 7.5. Frequently asked questions?


7.5. Frequently asked questions?

Q:

So JSON is just another data format, like XML?

A:

That's right. Any time you send information between your web page and a server, you're going to need some way to format that information. So far, you've used plain text to send requests, and text and XML to send responses. JSON is just one more way to send data back and forth.

Q:

If we've already got XML and text as options, why do we need JSON?

A:

A lot of JavaScript programmers aren't used to looking at or writing XML. Even though the DOM hides a lot of the details of XML, JSON still feels a lot more like arrays and lists to JavaScript programmers than XML does. If you know both XML and JSON, then you're way ahead of the curve, and can choose whichever format you like.

Q:

I'm a little confused by all those curly braces. Can you explain how they work again?

A:

The curly braces, { and }, contain unordered sets of values, and the [ and ] characters indicate an ordered array. Take a look:

Q:

So I should convert my version of the Boards 'R' Us application to use JSON?

A:

That's really up to you. We've created a version of the Boards app which gets JSON from the server. You can check it out online at http://www.headfirstlabs.com/books/hrajax/chapter07/ boards/boards.html. You can also check out the code we used in the chapter07/boards/ folder in the examples.

But there's nothing that says you need to use JSON in your version of the Boards app, or in your own apps. It's just an option, along with XML and text. The choice is really yours.

Q:

But which is better? XML or JSON?

A:

It really depends on your app. Lots of times, JSON looks and feels a little more like working with rows in a table, or arrays... but XML can represent complex data structures pretty easily, too. The choice is up to you!

 The top-level item is named "totals". {"totals": [ This opening bracket indicates the beginning of an array. So you can access each row in "totals". totals[0]{"location":"Vail", "boardsSold":642, "bootsSold":86, "bindingsSold":19}, You use the name of the array, and then the row number... ...and then the name of the item in that row you want the value for. totals[1]{"location":"Santa Fe", "boardsSold":236, "bootsSold":45, "bindingsSold":32}, totals[2]{"location":"Boulder", "boardsSold":453, "bootsSold":90, "bindingsSold" :16totals[2].bindingsSold}, totals[3]{"location":"Denver", "boardsSold":379,totals[3]. boardsSold "bootsSold":94, "bindingsSold":18} The array numbering starts at 0, not 1. Since each row has the items in curly braces, order doesn't matter. You just access the data in a row by name, like "location" or "bootsSold". 




Head Rush Ajax
Head Rush Ajax (Head First)
ISBN: 0596102259
EAN: 2147483647
Year: 2004
Pages: 241

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net