<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Save JSON in LocalStorage</title>
</head>
<body>
<script>
JSONstorageName = "my-json-LocalStorage";
JSONinit = {};
// Read as string convert and return as object
function loadJSON()
{
var Jdata = localStorage.getItem(JSONstorageName);
if (!Jdata)
Jdata = JSONinit; // INIT json OBJECT
else
Jdata = JSON.parse(Jdata); // Convert to and object
return Jdata; // JSON object
}
// Save as string
function saveJSON(value)
{
localStorage.setItem(JSONstorageName, JSON.stringify(value));
}
function delJSON()
{
localStorage.removeItem(JSONstorageName);
}
value = {"name":"Gideon","val":15.24};
saveJSON(value);
console.log(loadJSON().name);
delJSON();
</script>
</body>
</html>