使用 angular JSON.parse 位置 0 JSON 中的意外标记 o
Unexpected token o in JSON at position 0 at JSON.parse using angular
我看过模拟线程,但没有成功。我想做的是通过更新功能更新我的本地存储。函数如下所示:
调用变量的代码:
var localProfile = JSON.parse(localStorage.getItem("profile"));
if(localProfile != undefined && localProfile.length>0)
{ this.profiles = localProfile; }
else {
this.profile = [
{ id: "1526", name: "berserk", password: "berserk", age: "31", gender: "male"},
{ id: "1358", name: "Johnathan", password: "test", age: "17", gender: "male"},
{ id: "2539", name: "Britney", password: "test", age: "18", gender: "female"},
{ id: "1486", name: "Kevin", password: "test", age: "7", gender: "male"},
{ id: "7777", name: "jesus", password: "holy", age: "unknown", gender: "male"},
{ id: "6666", name: "satan", password: "hell", age: "unknown", gender: "unknown"}
];
}
更新变量的代码:
this.updateProfile = function(profile) {
profile.updating = false;
console.log(profile);
localStorage.setItem("profile", profile);
}
正如我在标题中指出的那样,我目前正在使用 Angular。我使用了 console.log(
行,响应似乎完全符合预期。我试过使用 JSON.parse(
和 JSON.stringify
以及其他几个组合。尝试重新加载页面时,我似乎遇到了上述错误或其他错误。显然我要么无法执行该语句,要么最终破坏了数据,因此重新加载页面 returns 一个类似的错误。
如果变量配置文件中的数据有疑问:
Array [ Object, Object, Object, Object, Object, Object ]
仔细查看数据时:
age:"17"
gender:"male"
id:"1358"
name:"johnathan"
password:"test"
另一个 object 看起来一模一样,没有奇怪的默认值。我已经处理好 $$hashkey 以防出现问题。
非常感谢任何有关如何正确执行调用的帮助,如果信息不足,请告知。
问题是您在保存数据时没有使用 JSON.stringify。因此,当您从 localStorage 解析时,它不是 json。
添加要在处理 JSON.parse()
和 JSON.stringify()
的应用程序中使用的工厂
.factory('LocalStorageUtil', function() {
return {
get: get,
set: set,
remove: remove
}
function get(key) {
return JSON.parse(localStorage.getItem(key));
}
function set(key, val) {
localStorage.setItem(key, JSON.stringify(val));
}
function remove(key) {
return localStorage.removeItem(key);
}
})
这是一个使用此 LocalStorageUtil 的 JS Bin 微型示例应用程序。
我看过模拟线程,但没有成功。我想做的是通过更新功能更新我的本地存储。函数如下所示:
调用变量的代码:
var localProfile = JSON.parse(localStorage.getItem("profile"));
if(localProfile != undefined && localProfile.length>0)
{ this.profiles = localProfile; }
else {
this.profile = [
{ id: "1526", name: "berserk", password: "berserk", age: "31", gender: "male"},
{ id: "1358", name: "Johnathan", password: "test", age: "17", gender: "male"},
{ id: "2539", name: "Britney", password: "test", age: "18", gender: "female"},
{ id: "1486", name: "Kevin", password: "test", age: "7", gender: "male"},
{ id: "7777", name: "jesus", password: "holy", age: "unknown", gender: "male"},
{ id: "6666", name: "satan", password: "hell", age: "unknown", gender: "unknown"}
];
}
更新变量的代码:
this.updateProfile = function(profile) {
profile.updating = false;
console.log(profile);
localStorage.setItem("profile", profile);
}
正如我在标题中指出的那样,我目前正在使用 Angular。我使用了 console.log(
行,响应似乎完全符合预期。我试过使用 JSON.parse(
和 JSON.stringify
以及其他几个组合。尝试重新加载页面时,我似乎遇到了上述错误或其他错误。显然我要么无法执行该语句,要么最终破坏了数据,因此重新加载页面 returns 一个类似的错误。
如果变量配置文件中的数据有疑问:
Array [ Object, Object, Object, Object, Object, Object ]
仔细查看数据时:
age:"17"
gender:"male"
id:"1358"
name:"johnathan"
password:"test"
另一个 object 看起来一模一样,没有奇怪的默认值。我已经处理好 $$hashkey 以防出现问题。
非常感谢任何有关如何正确执行调用的帮助,如果信息不足,请告知。
问题是您在保存数据时没有使用 JSON.stringify。因此,当您从 localStorage 解析时,它不是 json。
添加要在处理 JSON.parse()
和 JSON.stringify()
.factory('LocalStorageUtil', function() {
return {
get: get,
set: set,
remove: remove
}
function get(key) {
return JSON.parse(localStorage.getItem(key));
}
function set(key, val) {
localStorage.setItem(key, JSON.stringify(val));
}
function remove(key) {
return localStorage.removeItem(key);
}
})
这是一个使用此 LocalStorageUtil 的 JS Bin 微型示例应用程序。