【发布时间】:2016-09-20 13:04:27
【问题描述】:
我目前有一个我正在尝试使用的 JSON 对象。这是我对其进行字符串化的原因,因为 localStorage 只能有字符串:
$http.post('http://localhost:8000/refresh', {
name: $scope.name,
email: $scope.email,
token: $rootScope.devToken,
platform: ionic.Platform.platform()
}).then(function(response) {
console.log("Saving profile");
window.localStorage.setItem("UserProfile", JSON.stringify(response.data));
$state.go('home');
});
当我console.log(response.data)时,正确的数据就出来了。
然后这是我从 localStorage 中取出它的:
var temp = window.localStorage.getItem("UserProfile");
var profile = JSON.parse(temp);
console.log("Profile: " + profile);
当我 console.log(profile) 我得到 Object 对象。我究竟做错了什么?当我console.log(temp) 我得到一大串正确的数据。但我不希望它是一个字符串。我需要它回到一个对象。
编辑: JSON:
[
{
userProfileID: 1,
firstName: 'Austin',
lastName: 'Hunter',
email: 'ahunasdfgk.com',
token: '',
platform: '',
password: 'inc3',
companyProfileID: 1,
authentication: '',
UserTopics: [
{
topicID: 1,
topicName: 'Needs Maintenance',
alertLevel: 'Urgent',
TopicDepartments: [
{
departmentID: 1,
departmentName: 'Shop',
required: false,
DepartmentUsers: [
{
userProfileID: 1,
firstName: 'Austin',
lastName: 'Hunter',
email: 'ahunook.com',
token: '',
platform: '',
companyProfileID: 1
}, {
userProfileID: 2,
firstName: 'Ashley',
lastName: 'Jeanette',
email: 'ashlhgfdail.com',
token: '',
platform: '',
companyProfileID: 1
}
]
}
]
}, {
topicID: 2,
topicName: 'Help',
alertLevel: 'Urgent',
TopicDepartments: [
{
departmentID: 1,
departmentName: 'Shop',
required: false,
DepartmentUsers: [
{
userProfileID: 1,
firstName: 'Austin',
lastName: 'Hunter',
email: 'afafaf@oook.com',
token: '',
platform: '',
companyProfileID: 1
}
]
}, {
departmentID: 2,
departmentName: 'Office',
required: false,
DepartmentUsers: [
{
userProfileID: 1,
firstName: 'Ashley',
lastName: 'Jeanette',
email: 'ashfafaff.com',
token: '',
platform: '',
companyProfileID: 1
}
]
}
]
}
]
}
];
console.log(profile) 给了我这个:
[Log] Array (1) (controllers.js, line 65)
0 Object
UserTopics: [Object, Object] (2)
_id: "57e078cc62e223290851c2c1"
authentication: ""
companyProfileID: 1
email: "ahun______.com"
firstName: "Austin"
lastName: "Hunter"
password: "inco_______23"
platform: ""
token: ""
userProfileID: 1
Object Prototype
__defineGetter__(propertyName, getterFunction)
__defineSetter__(propertyName, setterFunction)
__lookupGetter__(propertyName)
__lookupSetter__(propertyName)
constructor: function()
hasOwnProperty(propertyName)
isPrototypeOf(property)
propertyIsEnumerable(propertyName)
toLocaleString()
toString()
valueOf()
Array Prototype
No Properties.
Object Prototype
__defineGetter__(propertyName, getterFunction)
__defineSetter__(propertyName, setterFunction)
__lookupGetter__(propertyName)
__lookupSetter__(propertyName)
constructor: function()
hasOwnProperty(propertyName)
isPrototypeOf(property)
propertyIsEnumerable(propertyName)
toLocaleString()
toString()
valueOf()
【问题讨论】:
-
删除日志中的字符串 - 这就是日志输出的方式。
console.log(profile)- 或console.log("profile", profile) -
您在记录时将对象转换为字符串,结果是
"[Object object]"... 完全错了。 -
那行得通。访问数据还是一样吗? profile.email 还是 profile.name?
-
对于任何有我同样问题的人,让我澄清一下。我有一个对象数组,里面有对象数组。我不能只做 profile.email 的原因是因为它是一个数组。我必须做 profile[0].email 才能收到电子邮件。谢谢大家的帮助!
标签: javascript angularjs json ionic-framework local-storage