JSON Basics - Root element with students and their attributes
(Page 5 of 5 )
The root element is the object with the three students arranged in an array. Each of the students is an object as shown in the next listing. This is an object whose value is an array. The wclass[1] refers to the second student object and is accessed as shown in the alert statement. This code will display an alert window with value 2.
<SCRIPT LANGUAGE="JavaScript">
var justStudents={"wclass":[
{"student":{"id":"1"}},
{"student":{"id":"2"}},
{"student":{"id":"3"}}
]};
alert(justStudents.wclass[1].student.id);
</SCRIPT>
In the above code the children of the students are not included.
Root element with student's children
The next listing shows the JSON object with only the children of students shown in an array. Here student 2 is "Charles Boyer" and his legacyskill is displayed in the alert box.
<SCRIPT LANGUAGE="JavaScript">
var webclass ={
"wclass":[
{"student":
{"name":"Linda Jones", "legacySkill":"Access, VB 5.0"}},
{"student":
{"name":"Adam Davidson", "legacySkill":"Cobol, MainFrame"}},
{"student":
{"name":"Charles Boyer", "legacySkill":"HTML, XML"}}
]
};
alert(webclass.wclass[1].student.legacySkill);
</SCRIPT>
Combining the two, it is easy to see how the JSON equivalent of webstudents.xml can be fashioned as shown in the next listing. The listing also shows how the various values are accessed.
<SCRIPT LANGUAGE="JavaScript">
var studentAll={"wclass":[
{"student":{"id":"1"},"name":"Linda Jones",
"legacySkill":"Access, VB 5.0"},
{"student":{"id":"2"},"name":"Adam Davidson",
"legacySkill":"Cobol, MainFrame"},
{"student":{"id":"3"},"name":"Charles Boyer",
"legacySkill":"HTML, XML"}
]};
document.write("<b>Id of 3rd student: </b>" + studentAll.wclass[2].
student.id);
document.write("<br/>");
document.write("<b>name of 2nd student:</b> " + studentAll.wclass[1].
name);
document.write("<br/>");
document.write("<b>legacySkill of 1st student:</b> " + studentAll.
wclass[0].legacySkill);
</SCRIPT>
The displayed output when this script is executed is as shown in the next picture. Notice how the student attributes are accessed.

Summary
The tutorial has introduced JSON, JavaScript Object Notation (RFC 4627) with examples of its types, objects and structures. JSON is well suited for data interchange. JSON is stable because it has no versions; needs no validation; and is not extensible, all very likable characteristics which may make it a long time player in JavaScript. However, since JSON is a subset of JavaScript, it has to conform to the same naming conventions regarding the use of language specific keywords and other rules.
Also, its usefulness in AJAX calls is obvious because JSON is as easy for humans as it is for machines, and a lot simpler and more understandable. In the simple example of webstudents.xml, the number of characters for XML amounted to 413, whereas when formatted in JSON it required 243, a substantial difference. Accessing values is also a lot simpler than going through the ECMA object model climbing up and down the XML tree. Future articles will deal with other features of JSON.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |