abc

Share This blog with your friends, so that we can improve more & more . our aim is to easy & simple way of learning.

12/13/2020

how create JSON variable and store to localhost

 i) create JSON - JavaScript object notation(JSON) can be defined in curly brace, & then stringify to variable.


myObj = { name: "John", age: 31, city: "New York" };

myJSON = JSON.stringify(myObj);


ii) store value to local Storage or database


localStorage.setItem("testJSON", myJSON); OR save this json to database


iii) get saved data 


text = localStorage.getItem("testJSON");

obj = JSON.parse(text);

document.getElementById("hello").innerHTML = obj.name;



final code likes below,

<!DOCTYPE html>

<html>

<body>


<h2>Store and retrieve data from local storage.</h2>


<p id="hello"></p>


<script>

var myObj, myJSON, text, obj;


// Storing data:

myObj = { name: "John", age: 31, city: "New York" };

myJSON = JSON.stringify(myObj);

localStorage.setItem("testJSON", myJSON);


// Retrieving data:

text = localStorage.getItem("testJSON");

obj = JSON.parse(text);

document.getElementById("hello").innerHTML = obj.name;

</script>


</body>

</html>


No comments:

Post a Comment

An Introduction to the Laravel Framework: What It Is and Why You Should Use It

  If you're a PHP developer looking for a modern, efficient, and powerful framework to build web applications, look no further than Lara...