<< Chapter < Page | Chapter >> Page > |
Listing 16 . Json0130a.htm.
<!--01/26/14
Illustrates the differencebetween the syntax of a
JavaScript object and a JSON string that looks a lot likea JavaScript object.
Must parse the JSON string to turn it into a JavaScriptobject before processing it using JavaScript.
Uses JSON.stringify to produce a JSON string from aJavaScript object.
Uses JSON.parse to produce a JavaScript object froma JSON string.--><!DOCTYPE html><html><head><title>ParseJSON01</title></head><body><script>//Define a function
function aMethod(){return true;};document.write("Create a JavaScript object.");
var obj01 = {name:"Bill",age:31,method:aMethod};document.write(
"<br/>Unsuccessful attempt to display object.");
document.write("<br/>" + obj01);
document.write("<br/>Display keys in object: ");
for (var key in obj01) {if (obj01.hasOwnProperty(key)) {
document.write(key + " ");}//end if
}//end for loopdocument.write("<br/>Display values in object: ");
document.write(obj01.name + ", "+ obj01.age + ", " + obj01.method());
document.write("<br/>Transform JavaScript object " +
"into a JSON string.");// Note that the method does not become part of the
// JSON string.var jsonstring = JSON.stringify(obj01);
document.write("<br/>Display JSON string " + jsonstring );
document.write("<br/>Unsuccessful attempt to " +
"access name and age.");document.write("<br/>" + jsonstring.name + ", "
+ jsonstring.age);document.write("<br/>Transform the JSON string " +
"into a JavaScript object.");var obj02 = JSON.parse(jsonstring);
document.write("<br/>Display values in object: ");
document.write(obj02.name + ", " + obj02.age);document.write("<br/>Display keys in object: ");;
// Note that it does not contain the method from the// original JavaScript object.
for (var key in obj02) {if (obj02.hasOwnProperty(key)) {
document.write(key + " ");}//end if
}//end for loop</script></body></html>
Listing 17 . Json0130b.htm. |
---|
<!--01/26/14---------------------------------------------//
Illustrates the difference between the syntax of aJavaScript object a JSON string that looks a lot like a
JavaScript object where each involves array data. Mustparse the JSON text to turn it into a JavaScript object
before processing it using JavaScript. --><!DOCTYPE html><html><head><title>ParseJSON02</title></head><body><script>document.write("Create a JavaScript object " +
"involving array data.");var obj01 = {friends:[
{name:"Bill",age:31},{name:"Jill",age:40}]};
document.write("<br/>Display values in object.");
document.write("<br/>" + obj01.friends[0].name +":" + obj01.friends[0].age);document.write("<br/>" + obj01.friends[1].name +":" + obj01.friends[1].age);document.write("<br/>Transform the object into a JSON string.");
var jsontext = JSON.stringify(obj01);document.write("<br/>Display JSON string.");
document.write("<br/>" + jsontext);
document.write("<br/>Transform the JSON string " +
"into a JavaScript object.");var obj02 = JSON.parse(jsontext);
document.write("<br/>Display values in object.");
document.write("<br/>" + obj02.friends[0].name+ ":" + obj02.friends[0].age);document.write("<br/>" + obj02.friends[1].name+ ":" + obj02.friends[1].age);</script></body></html> |
Listing 18 . Json0130d.htm. |
---|
<!DOCTYPE html><html><body><script>var jScrObj01 = {"name":"John","age":33};
var jSonStr01 = '{"name":"Bill","age":33}';document.write("<br/>1. " + jScrObj01.name);
document.write("<br/>2. " + jSonStr01.name);document.write("<br/>3. " + jScrObj01);
document.write("<br/>4. " + jSonStr01);var jScrObj02 = JSON.parse(jSonStr01);
var jSonStr02 = JSON.stringify(jScrObj01);document.write("<br/>5. " + jScrObj02.name);
document.write("<br/>6. " + jSonStr02.name);document.write("<br/>7. " + jScrObj02);
document.write("<br/>8. " + jSonStr02);</script></body></html> |
-end-
Notification Switch
Would you like to follow the 'Introduction to xml' conversation and receive update notifications?