HTML and CSS Reference
In-Depth Information
Figure 12-6 Converting a JavaScript object to JSON.
parse(json)
Now that you know how to convert JavaScript objects to JSON, let's look at how you can convert JSON back to
JavaScript objects. This is done using the parse() function. Simply pass this function your JSON, and it will re-
turn your object(s).
Try converting the JSON you have stored in the jsonPerson variable back to a JavaScript object. Enter the fol-
lowing into your console.
JSON.parse(jsonPerson);
This outputs an object into the console. You could also initialize a new variable to hold this object if you so desired.
Figure 12-7 shows the JavaScript object that has been parsed from the JSON.
Figure 12-7 Converting a JSON string to a JavaScript object.
Objects in LocalStorage
More complex types of data such as JavaScript objects cannot be stored in the same way as text and numbers. If you
tried to store a normal JavaScript object, it would be converted to a string and saved. The problem is that you cannot
Search WWH ::




Custom Search