(기초)그래서 뭘 배운거야?/JSON
JSON-07-JSON.stringify() : localStorage저장 후 사용하기
Soheny.P
2021. 10. 19. 17:17
728x90
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<p id="demo"></p>
<script>
// 객체 주어짐
const myObj = {name:"Soheny", age:31, city:"Newyork"};
// JSON으로 받아 문자열로 반환
const myJSON = JSON.stringify(myObj);
// localStorage에 반환값 설정
localStorage.setItem("testJSON", myJSON);
let text = localStorage.getItem("testJSON");
// JSON으로 다시 객체화
let obj = JSON.parse(text);
console.log(typeof obj); // Object
document.getElementById("demo").innerHTML = obj.name;
</script>
</body>
</html>
728x90