JSON can be built on two structures.
- A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
- An ordered list of values. In many languages, we have observed as an array, list, or sequence.
We have two inbuilt JSON objects which are actually supported by all browsers after IE 7.
NOTE: If you want to give JSON support to IE7 then download json2.js from json.org
Storing JSON Data in Arrays
In order to do this, we can enclose multiple objects in square brackets, which indicate an array. For instance, if I needed to include details about two users in one variable, we might be using the following way:
To access this information, we will need to access the array index of the user which we want to access. For example, we would need to use the following code to access info which is stored in families variable:
document.write(families.name); // Output: Kyle
document.write(families.age); // Output: 24
NOTE: This is useful for user’s if it will be necessary to loop through the stored information of variable, as it lends itself to a for loop with an automatically incrementing value.
2. LOCAL STORAGE
It is an Object to hold key & value pair init, Local Storage are not cookies, It is a browser property to store any value and the major difference between cookie & local storage is a server can access cookie but can’t local storage. The read-only localStorage property allows you to access a Storage object for the Document’s origin; the stored data is saved across browser sessions. localStorage is similar to sessionStorage, except that while data stored in localStorage has no expiration time, data stored in sessionStorage gets cleared when the page session ends — that is, when the page is closed.
In simple terms, all that web storage does is to store named key/value pairs locally and unlike cookies, this data persists even if you close your browser or turn off your computer.
We can add local storage in two ways:
localStorage.userName = “sush” // localstorage to set an property for localstorage
localStorage.setValue(‘age’, 25); // here we have declared an Integer localStorage will convert into string to use as Integer we need to convert by our own using parseInt();
To remove the item from local storage :
localStorage.removeValue(‘age’); // is to remove an Item from Local Storage.
We have another method available to clear all localStorage currently exists on the browser.
3. Error Handling
The try statement tests a block of code for errors.
The catch statement handles the error.
The throw statement will create custom errors.
The finally statement forces execute code, after try and catch, for the releasing memory & resources.
console.log(err.message); // Output: showMessage is not defined
The above written code tells us that when try encounters an error, it immediately skips any remaining code inside it and goes straight to catch. By default, the error will be displayed that is obviously suppressed, now though we can still fetch this information by accessing the Error object that gets indirectly sent to catch statement.
Note: We can have nested try catch statements to handle the very complex errors & statements.
throw : Throw statement is for creating custom errors. We can also explicitly throw our own exceptions to force that to happen on demand. At the time of waiting for 1 of the six types of errors user get above statement to occur before control, it is automatically transferred from the try block to the catch block. It is great for creating your own definitions of an error is and how control should be transferred to catch. In following ways, you can throw an error:
1. throw “An error has occurred”
2. throw true
3. throw new Error(“I detect an error!”)
4. throw new SyntaxError(“Your syntax is no good”);
Happy Coding!!! For more interesting blogs, click here
- Difference Between SQL and MySQL - April 14, 2020
- How to work with Subquery in Data Mining - March 23, 2018