我目前有一个要使用的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)
得到对象对象时。我究竟做错了什么?当我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: '[email protected]',
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)
。您的方法将“配置文件”转换为String。因此给出[Object Object]
。对于您的情况,要获取该信息,email
请使用profile [0] .email,因为您的调用返回一个数组而不是JSON对象。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句