입니다, 나는문자열이 누군가를 위해 관심있는 stringify'd JSON 객체
http://plugins.jquery.com/project/localcache 내가 뭘하는지, 여기에 "만료와 로컬 스토리지"스크립트를를 구축 결국 사용자가이 할 수 있도록 저장 :
localStorage.setThing(key, value)
을 사용자가 다음을 수행 할 수 있습니다 내 setThing의 메타에서
localStorage.setThing("key1", 1)
localStorage.setThing("key2", "this is a string")
localStorage.setThing("key3", { prop1: "this is a json obj" })
od, typeof 값을 확인하고 있는데 typeof value == "object"
인 경우 localStorage.setItem(key, JSON.stringify(value))
getThing 메서드에서 localStorage로 만드는 값은 항상 문자열이됩니다. 그래서, 어떻게해야합니까?
var val = localStorage.getItem("key3")
if (val is a previously JSON.stringify'd object) // <-- ??
return JSON.parse(val)
내가 발에 정규식 체크를 할 필요가 있는가, 그렇다면, 사람이 문자열 정말 JSON.stringify'd 객체 인 경우 나에게 말한다 편리 패턴이 있습니까?
감사합니다.
EAFP를 사용해보십시오 - 용서를 구하는 것보다 용서를 구하는 것이 더 쉽습니다) - * 패턴 * [Grace Hopper], http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Exceptions을 참조하십시오. – miku
예가 있습니까? –