<< Chapter < Page | Chapter >> Page > |
Let's pretend
For simplicity, I combined the disassembly and the reassembly of the object into a single script. Let's pretend, however, that the code discussed downto this point resides on Computer-A and the remaining code resides on Computer-B at a different location. Pretend that the JSON stringcreated in Listing 6 has been transported from Computer-A to Computer-B. Now its time for the code inComputer-B to use that JSON string to replicate the original object.
Transform the JSON string into a JavaScript object
The code in Listing 8 calls the JSON.parse method passing the JSON string as a parameter to create a replica of the original JavaScript object that wascreated on Computer-A.
The JSON.parse method uses the JSON string to reassemble and return an object that is a replica of the originalobject that was created in Listing 2 (minus the method property, which was lost in the creation of the JSON string in Listing 6 ) .
Then Listing 8 uses code similar to that shown in Listing 5 to access and display the values of the remaining two properties of the object as shown by thesecond line in Figure 3 .
Finally, the code in Listing 9 uses code similar to that shown earlier in Listing 4 to display the keys in the new object. The result is shown in the last line in Figure 3 where the keys are name and age with the method key missing.
Listing 9 . Display keys in object. |
---|
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> |
Listing 9 also signals the end of the script.
Listing 17 presents a similar but slightly more complicated script than the one discussed earlier. As before, I will discuss the code in fragments.
Output from the script
Figure 4 shows the screen output produced by opening this file in Firefox v26 or later.
Once again, I manually inserted some blank lines to make it easier on theeyes.
Figure 4 . Screen output from Json0130b,htm. |
---|
Create a JavaScript object involving array data.
Display values in object.Bill:31
Jill:40Transform the object into a JSON string.
Display JSON string.{"friends":[{"name":"Bill","age":31},{"name":"Jill","age":40}]}Transform the JSON string into a JavaScript object.
Display values in object.Bill:31
Jill:40 |
Create a JavaScript object
Listing 10 shows the creation of a JavaScript object and the display of the property values contained in that object.
Listing 10 . Create a JavaScript object. |
---|
<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); |
One property is an array
Notification Switch
Would you like to follow the 'Introduction to xml' conversation and receive update notifications?