<< Chapter < Page | Chapter >> Page > |
The new JSON string is saved in the variable named jsonstring .
The method has been lost
First note that as indicated earlier, this simple version of the JSON.stringify method discards methods belonging to the object when transforming it to a JSON string. Therefore, from this point forward in thescript, the method belonging to the original JavaScript object has been lost.
Note the similarity
Once again, note the similarity between the JSON string shown in the second line of Figure 2 and the object literal version of the JavaScript object shown in Listing 2 .
Having discarded the method, the only difference between the two is that the keys in the JSON string are enclosed in quotes while the keys in Listing 2 are not enclosed in quotes.
As I understand it, the keys in a JSON string must always be enclosed in quotes while quotes are normally optional for keys in the object literal declaration of a JavaScript object. (Some keys must be enclosed in quotes in the object literal syntax for a JavaScript object.)
Unsuccessful attempt to access name and age
However, even though the syntax is very similar, a JSON string is very different from a JavaScript object. A JavaScript object is a type having contentand behavior. A JSON string is just a string of characters having content but no behavior. This is illustrated by the code in Listing 7 , which is very similar to the code in Listing 5 .
Listing 7 . Unsuccessful attempt to access name and age. |
---|
document.write("<br/>Unsuccessful attempt to " +
"access name and age.");document.write("<br/>" + jsonstring.name + ", "
+ jsonstring.age) |
When dot notation was used to access the name and age properties of the object in Listing 5 , the values of those properties were returned and displayed on Line 5 in Figure 1 .
When a similar syntax was used in an attempt to access the values associated with name and age in Listing 7 , the result was "undefined" as shown on the fourth line of Figure 2 . In other words, a JSON string is just what it says; simply a string of characters.
The magic of a JSON string
To the extent that there may be magic, the magic of the JSON string is
Cleanup time again
Let's create one more simplified Figure showing the script output. Figure 3 shows the last three lines of text from Figure 1 that haven't been discussed yet.
Figure 3 . Partial screen output from Json0130a.htm. |
---|
Transform the JSON string into a JavaScript object.
Display values in object: Bill, 31Display keys in object: name age |
Transform the JSON string into a JavaScript object
The first two lines of text in Figure 3 were produced by the code in Listing 8 .
Listing 8 . Transform the JSON string into a JavaScript object. |
---|
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); |
Notification Switch
Would you like to follow the 'Introduction to xml' conversation and receive update notifications?