js中实现json格式的转换
2021-07-14 02:05
标签:htm his ring json格式 gif log highlight pre 对象 js中实现json格式的转换 标签:htm his ring json格式 gif log highlight pre 对象 原文地址:http://www.cnblogs.com/1020182600HENG/p/7077209.htmlfunction person(id,name,age){
this.id=id;
this.name=name;
this.age=age;
}
var p=new person(1001,‘tom‘,27);
//转换为JSON格式 [{"id":1001,"name":"tom","age":27},{}...{}]
var jsonStr=JSON.stringify(p);
//将JSON格式解析为字符串
var strJson=JSON.parse(jsonStr);
//存储到localStoreage中
localStorage.person=jsonStr;
//从localStorage 中读取对象数据
var ls=localStorage.person;