-
Notifications
You must be signed in to change notification settings - Fork 0
Open
Description
var obj={"a":1,"b":2};
localStorage.setItem("temp2",obj);
typeof localStorage.getItem("temp2");也会返回String
localStorage.getItem("temp");却返回[object Object]
用localStorage.setItem()正确存储json对象方法是:存储前先用JSON.stringify()方法将json对象转换成字符串形式,如:
var obj={"a":1,"b":2};
obj=JSON.stringify(obj);
localStorage.setItem("temp2",obj);
typeof localStorage.getItem("temp2");返回String
localStorage.getItem("temp2");返回字符串格式:{"a":1,"b":2}
后续要操作该json对象,自然得将之前存储的json字符串先转成json对象再进行操作,如:
obj=JSON.parse(localStorage.getItem("temp2"));
操作完,存储信息前,记得再转换下格式:obj=JSON.stringify(obj);
Metadata
Metadata
Assignees
Labels
No labels