json Flashcards
What is JSON?
data interchange format used to send and store information in computer systems; text-based data format; exists as a string and needs to be converted to a native js object when you want to access the data
What are serialization and deserialization?
serialization: converting a native object to a string so it can be transmitted across the network
deserialization: converting a string to a native object
Why are serialization and deserialization useful?
transmit data across a network
How do you serialize a data structure into a JSON string using JavaScript?
JSON.stringify: converts a JavaScript object or value to a JSON string
How do you deserialize a JSON string into a data structure using JavaScript?
JSON.parse: parses a JSON string, constructing the JavaScript value or object described by the string
json and strings
JSON requires double quotes to be used around strings and property names. Single quotes are not valid other than surrounding the entire JSON string
simplest json
a single string or number would be valid JSON; can actually take the form of any data type that is valid for inclusion
How to you store data in localStorage?
first, use the stringify method of the json object to turn the data (the argument of the method) to a string
then use the
set item method of the local storage object with two arguments, key name and key value
How to you retrieve data from localStorage?
get item method of the local storage object with the kay name as the argument
What data type can localStorage save in the browser?
strings; not objects nor arrays
When does the ‘beforeunload’ event fire on the window object?
when the window, the document and its resources are about to be unloaded; a confirmation dialog asking the user if they really want to leave the page; to show the confirmation dialog an event handler should call preventDefault() on the event
setItem
arg:
- keyname: A DOMString containing the name of the key you want to create/update.
- key value: A DOMString containing the value you want to give the key you are creating/updating.
return: undefined
getItem
arg: A DOMString containing the name of the key you want to retrieve the value of
return: A DOMString containing the value of the key. If the key does not exist, null is returned
use a if statement after get
Window.localStorage
The localStorage read-only property of the window interface allows you to access a Storage object for the Document’s origin; the stored data is saved across browser sessions; value: a Storage object which can be used to access the current origin’s local storage space
how to clear local storage?
use the delete method, i.e., localStorage.clear();
set the data to empty set