[size=large]最近在做party_bid中处理报名信息那块,由于存的数据结构比较复杂,取数据时经常取不到,现在小结一下。[/size]
[size=medium]先简单介绍一下localStorage的基本使用方法:[/size]
[size=medium]如果localStorage中存储数据比较复杂时,例如存储的信息为:
displayed_bid_activity 竞价1
竞价1 {"status":"start","bid_applicants":[{"name":"胡","phone":"12345678902","price":"131"}]}[/size]
[size=medium]如果想知道现在进入的竞价活动的所有信息,如果执行:[/size]
[size=medium]如果想知道竞价活动的状态信息:[/size]
[size=medium]想知道竞价人信息:[/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;