2014-12-16 2 views
0

내가있는 경우 :어떻게 로컬로 저장된 JSONstringify 객체를 html로 인쇄 할 수 있습니까?

var Store = localStorage("Storage", JSON.stringify({ 
    name: "Bob", 
    age: 30 
})); 

이 어떻게 자바 스크립트를 사용하여 내용을 인쇄 할 수 있습니까? 나는 또한이 질문을했다 : https://stackoverflow.com/questions/27502969/how-to-load-the-localstorage-json-strinigfy-objects-to-html-rows, 그러나 지금까지 응답 없음. 나는 내 질문을 더 간단하게하려고 노력하고있다.

+0

어떤 경고'에 대해 (localStorage.getItem를 ('저장'))'? 경고 부분을 다른 것으로 바꾸어 HTML 또는 기타로 인쇄 할 수 있습니다. –

+0

'localStorage'는 함수라고 생각하지 않습니다. HTML로 데이터를 표시하는 것은'document.getElementById ('yourOutputElement')처럼 간단합니다 .textContent = jsonString; –

답변

0

localStorage 개체에서 .getItem() 또는 .setItem()을 호출해야합니다.

이미 로컬 스토리지에 저장된 항목을 인쇄하려면 개체

var obj = JSON.parse(localStorage.getItem('Storage')); 

를 검색하려면 객체

localStorage.setItem('Storage', JSON.stringify({ name: 'Bob', age: 30})); 

을 저장하려면

var obj = localStorage.getItem('Storage'); // Get the item 
console.log(obj) // The item is currently a String 
console.dir(JSON.parse(obj)) // JSON.parse converts the String into an Object 
0

localStorage는 함수가 아닙니다. 그것은 단순한 객체이며, "문자열 화"될 수 있습니다. 쓰기, localStorage[ "Storage" ] = { name: "Bob", age: 30 };

그것을 얻으려면 : 다음 var Store = JSON.stringify(localStorage[ "Storage" ]);

그것을 인쇄 :

작성, 저장 값을 설정하려면 document.getElementById("myElement").innerHTML = Store;

관련 문제