Working with JSON (Javascript Object Notation) Objects
JSON is a data interchange format for storing objects. JSON is language-independent, but is derived from JavaScript. Using JavaScript, we can do the following:
- Use JSON.stringify(x) to convert object 'x' into a JSON (string) format
- Use JSON.parse(x) to convert the JSON object 'x' into a javascript object
- JSON is used to store objects in local storage:
- Use localStorage.setItem("key", x) to save the JSON object 'x' to local storage
- Use localStorage.getItem("key") to read the key (in JSON format) from local storage
For more information see https://www.w3schools.com/js/js_json_intro.asp
Example 1 (View source code to see how JSON.stringify is used)
JS code for creating mycourse object:
var mycourse = { course: "CSC 301", time: "MWF, 11:00 - 11:50" };
Example 2 (View source code to see how JSON.stringify is used)
JS code for creating prof object:
var prof = { firstName: 'Garrett', lastName: 'Dancik',
classes: ['CSC 202', 'CSC 301', 'CSC 314']};
Example 3
Wikipedia has an API that returns page titles and the first paragraph of an entry in JSON format, e.g., Eastern Connecticut State University
Example 4