JSON.parse()和JSON.stringify()

stringify()用于从一个对象解析出字符串;

 

var person = {name:"vanida","sex":"girl","age":25};

localStorage.setItem("person",JSON.stringify(person));

// localStorage.person="{"name":"vanida","sex":"girl","age":25}"

注意:JSON.stringify()中不要忘了“i”,stringify而不是stringfy!

 

 

parse用于从一个字符串中解析出json对象 可以用JSON.parse();

 

person = JSON.parse(localStorage.getItem("person"));

// {name: "vanida", sex: "girl", age: 25}

 

posted @ 2017-12-21 11:25  realDanielWu  阅读(312)  评论(0编辑  收藏  举报