localStorage.setItem() 不会自动将Json对象转成字符串形式
1.用localStorage.setItem()正确存储JSON对象方法是:
存储前先用JSON.stringify()方法将json对象转换成字符串形式
JSON.stringify() 方法可以将任意的 JavaScript 值序列化成 JSON 字符串
var list = [
{
title:"吃饭打豆豆",
isChecked:false
}
];
list = JSON.stringify(list); //转化为JSON字符串
localStorage.setItem("temp2",list);//localStorage.setItem存数据,返回{"title":'吃饭打豆豆',"isChecked":false }
2.后续要操作该JSON对象,要将之前存储的JSON字符串先转成JSON对象再进行操作
list=JSON.parse(localStorage.getItem("temp2"));//localStorage.getItem取数据