Javascript Data Type How to - Store JSON to local storage








Question

We would like to know how to store JSON to local storage.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){<!--   ww  w  .j  a  v a  2s. com-->
    var numberArray = [ "11","12","13","22","07","45" ];
    localStorage["numberArray"] = JSON.stringify(numberArray);
    var storedNumberArray = JSON.parse(localStorage["numberArray"]);
    document.write(storedNumberArray)
}
</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: