////////////////////////////
// Javascript and JSON
//
// JSON is the standard of exchanging information
// with remote servers / http servers.
//
// Mechanism for transforming complex objects into
// human readable string representations.
//
// It was made to replace XML as the format for exchanging
// data between servers (cleint / remote HTTP server.)
//
// V.Popular - AJAX communications with http servers
//
// Used for storing objects in LocalStorage. If we want to save
// a javascript object, we will have to turn it into JSON, then save
// it. When we read from the datastore, we retrieve the JSON,
// and turn it back into an object. Note: LocalStorage is
// the space reserved by the browser to be used as a small db for
// the application.
//
// Schematic of Function:
//
// Storing Object in LocalStorage:
// Object -> JSONstring -> Local Storage
//
// Retrieving Object from LocalStorage:
// LocalStorage -> JSONstring -> Object
//
// 1. object to JSON -> JSON.stringify(obj)
// 2. JSON to object -> JSON.parse(jsonString)
// ex 1 - simple int -> will return string int
var x = 3;
var JsonString1 = JSON.stringify(x);
console.log(JsonString1);
var JsonStringtoObj = JSON.parse(JsonString1);
console.log(JsonStringtoObj);
// Two major modules to fetch json data with ajax requests.
// The XHR2 API
// The Fetch API (Simpler)
////////////////////////////////////////////////
// FETCH API
function search() {
var queryURL = "https://jsonplaceholder.typicode.com/users";
fetch(queryURL)
.then(function(response) {
// response is a json string,
// convert it to a pure JavaScript object
// for the next 'then' callback.
return response.json();
})
.then(function(users) {
// users is a JavaScript object here
console.log(users)
})
.catch(function(error) {
console.log('Error during fetch: ' + error.message);
});
}
console.log(search());
JS.JSON.RetrievingData.FetchAPI.v1
----------------------------------
A [Pen](https://codepen.io/Onlyforbopi/pen/rqNJwE) by [Pan Doul](https://codepen.io/Onlyforbopi) on [CodePen](https://codepen.io).
[License](https://codepen.io/Onlyforbopi/pen/rqNJwE/license).