我目前有一个我想要使用的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(个人资料)给了我这个:
[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()
答案 0 :(得分:1)
尝试console.log(profile)
。您的方法将“配置文件”转换为String。因此给出了[Object Object]
。在您的情况下,要获取email
,请使用profile [0] .email作为您的调用返回数组而不是JSON对象。
答案 1 :(得分:1)
你的方法是正确的
console.log( profile); will print correct object
答案 2 :(得分:-1)