在我正在处理的程序中,我使用 JSON.stringify 将文字变量发送到本地存储。计划是我想不断更新本地存储并添加到现有的本地存储中。我在 JSON 文件的解析方面遇到问题。我添加到存储的代码是这样的:
function addtoStorage(key, data) {
if (typeof(Storage) !== "undefined") {
if (localStorage[key]) {
console.log("Local Storage stuff" + localStorage[key]);
var olddata = JSON.parse(localStorage[key]);
var dataJSON = JSON.stringify(olddata + data);
localStorage[key] = localStorage[key] + dataJSON;
}
else {
var dataJSON = JSON.stringify(data);
localStorage[key] = dataJSON;
}
}
else {
console.log("You don't have storage capabilities. Sorry. Next time improve your browser.");
}
} ;
我在 console.log 上的输出是:
Local Storage stuff{"asdf":"","tes":6,"type":"asdf","ast":1,"sd":"","ew":"","asdf":{"te":0,"wer":0},"asf":"","te":"","context":{"asdf":1,"total_hits":0,"asdf":1,"tew":0,"asdf":"","tes":"","date":"asfd-asdf-","asdf":0},"asdf":""}"[object Object][object Object]" main.js:487
未捕获的 SyntaxError:意外的字符串
我很确定我明白问题所在。我似乎无法弄清楚如何解决它。显然是过早地关闭了 JSON 对象,有什么建议吗???