<< Chapter < Page Chapter >> Page >

As you learned in an earlier module, several different programming languages including JavaScript, Java, and PHP support JSON. I elected to use JavaScriptin these modules because many of the students enrolled in this course are also enrolled in the web development curriculum at Austin Community College. Those students eitheralready have, or shortly will have a requirement to learn HTML and JavaScript programming fortheir other coursework.

If you already know HTML and you know how to program using JavaScript, you should continue with this module. If not, you need to take a side trip and learn how to program inJavaScript before continuing. Also, if you don't know HTML, you should learn that also.

There are hundreds of online tutorials available for learning HTML and JavaScript, some better than others. For HTML, I recommend the free online Introduction to HTML tutorial at (External Link) . For JavaScript, I recommend the free JavaScript tutorial at (External Link) An average college student should be able to complete either tutorial in about fifteen hours or less. Onceyou complete either or both tutorials, your knowledge of HTML and JavaScript should be sufficient for an understanding of JSON.

Differences between a JSON string and a JavaScript object

The syntax of a JSON string looks a lot like the syntax of a JavaScript object in object literal notation .

Compare the JSON string in the second line in Figure 2 with the object literal in the second line in Listing 2 .

This can be confusing to those who don't recognize the difference between the two. I will explain some of the differences in this module.

A JavaScript object is a type

A JavaScript object encapsulates data and methods and exhibits behavior. Despite the similarity of appearance, a JSON string is simply astring of characters with a well-defined format. It is not a type and it does not exhibit behavior.

At least it doesn't exhibit any behavior that is not expected of any other string.

Remember the playscape?

Harkening back to an earlier module, a JavaScript object is analogous to the playscape in the back yard that has swing and slide properties. The playscape can "do something" that many children find enjoyable.

A JSON string is analogous to the well-organized package of parts resulting from the disassembly of the playscape. In its disassembled state, the JSON stringdoes not have properties like swing and slide even though all of the parts necessary to support those properties are in the packageof parts. In that state, all it can do is lay there and take up space. It is unlikely that children would find it to be enjoyable.

I will refer back to the playscape a few more times in this module.

Transform a JSON string into a JavaScript object

In order to do much in the way of significant processing on the contents of a JSON string using JavaScript, you first need to transform it into a JavaScript object. (You need to reassemble the playscape before the children can play on it.)

If you are working in some other language, you need to transform it into a data structure that is appropriate for that language.

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Introduction to xml. OpenStax CNX. Dec 02, 2014 Download for free at https://legacy.cnx.org/content/col11207/1.18
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Introduction to xml' conversation and receive update notifications?

Ask