如何取出localStotage 中存储的信息

[size=large]最近在做party_bid中处理报名信息那块,由于存的数据结构比较复杂,取数据时经常取不到,现在小结一下。[/size]
[size=medium]先简单介绍一下localStorage的基本使用方法:[/size]
localStorage.a = 3;//设置a为"3"
localStorage["a"] = "sfsf";//设置a为"sfsf",覆盖上面的值
localStorage.setItem("b","isaac");//设置b为"isaac"
var a1 = localStorage["a"];//获取a的值
var a2 = localStorage.a;//获取a的值
var b = localStorage.getItem("b");//获取b的值
localStorage.removeItem("c");//清除c的值


[size=medium]如果localStorage中存储数据比较复杂时,例如存储的信息为:
displayed_bid_activity 竞价1

竞价1 {"status":"start","bid_applicants":[{"name":"胡","phone":"12345678902","price":"131"}]}[/size]

[size=medium]如果想知道现在进入的竞价活动的所有信息,如果执行:[/size]
var bid_activity_mes = localStorage.getItem("displayed_bid_activity")//这样取到的只是现在进入的竞价活动,也就是取到的“竞价1”,并不能取到“竞价1”中存储的竞价活动信息。

var bid_activity = localStorage.getItem("displayed_bid_activity");
//先把displayed_bid_activity中存储的“竞价1”赋值给变量bid_activity
var bid_activity_mes = JSON.parse(localStorage.getItem(bid_activity));
//注意此处的bid_activity不加引号,取到的是bid_activity所代表的“竞价1”中存的内容,由于localStorage中存储时会自动转为字符串,所以前面加上JSON.parse来恢复成之前的数组结构。

[size=medium]如果想知道竞价活动的状态信息:[/size]
var bid_activity = localStorage.getItem("displayed_bid_activity");
var bid_activity_mes = JSON.parse(localStorage.getItem(bid_activity);
status = bid_activity_mes.status这就可以取出“竞价1”的状态“start”

[size=medium]想知道竞价人信息:[/size]

var bid_activity = localStorage.getItem("displayed_bid_activity");
var bid_activity_mes = JSON.parse(localStorage.getItem(bid_activity)
var bid_activity_mes = JSON.parse(localStorage.getItem(localStorage.displayed_bid_activity));//这句和上面两句的效果是一样的。取到的结果是{"status":"start","bid_applicants":[{"name":"胡","phone":"12345678902","price":"131"}]}
var bid_activity_applicants = bid_activity_mes.bid_applicants;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值