繁体   English   中英

JSON 返回 Object 对象

[英]JSON returns Object Object

我目前有一个我正在尝试使用的 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)我得到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) 您的方法将“配置文件”转换为字符串。 因此给出[Object Object] 在您的情况下,要获取email请使用 profile[0].email 作为您的调用返回数组而不是 JSON 对象。

你的方法是正确的

console.log( profile); will print correct object

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM