admin管理员组文章数量:1291024
I have this JSON array, and what I want is to get the password field alone
var user = [ { _id: 5902086ecbc0dd11e4870fd9,
password: '$2a$08$FIpkmFT1WDZggQYyBA4CVuop6pelbKBfUEJ1/KAVIV2Si9Ho1EYhi',
email: '[email protected]',
lastName: 'v',
firstName: 'j',
updatedDate: 2017-04-27T15:04:14.483Z,
createdDate: 2017-04-27T15:04:14.483Z } ]
I tried to parse it using this code
var obj = JSON.parse(user);
console.log(user.password);
but still it is undefined.
I have this JSON array, and what I want is to get the password field alone
var user = [ { _id: 5902086ecbc0dd11e4870fd9,
password: '$2a$08$FIpkmFT1WDZggQYyBA4CVuop6pelbKBfUEJ1/KAVIV2Si9Ho1EYhi',
email: '[email protected]',
lastName: 'v',
firstName: 'j',
updatedDate: 2017-04-27T15:04:14.483Z,
createdDate: 2017-04-27T15:04:14.483Z } ]
I tried to parse it using this code
var obj = JSON.parse(user);
console.log(user.password);
but still it is undefined.
Share Improve this question edited Apr 27, 2017 at 15:58 Heretic Monkey 12.1k7 gold badges61 silver badges131 bronze badges asked Apr 27, 2017 at 15:55 JerViJerVi 1313 silver badges14 bronze badges 6-
It doesn't need parsing. It's already an array. And since it's an array of objects you'll have to use it like this:
user[ index ].password;
(user[0].password;
for the first object ...). – ibrahim mahrir Commented Apr 27, 2017 at 15:56 - 1 This is not JSON. There's nothing to parse! – Álvaro González Commented Apr 27, 2017 at 15:56
- Possible duplicate of What's the difference between Javascript Object and JSON object – Álvaro González Commented Apr 27, 2017 at 15:58
- Possible duplicate of Access / process (nested) objects, arrays or JSON – Heretic Monkey Commented Apr 27, 2017 at 15:58
- actually that is what I received from my db (I'm using moongose for that), and I dont know how will I get the password field. thanks for the response, it helps – JerVi Commented Apr 27, 2017 at 15:58
5 Answers
Reset to default 4User is already a list of objects, so you don't need to parse it. However, it is an array. So if you meant for it to be an array, you'd need to access the password by using this code:
console.log(user[0].password);
It's already an array there is nothing to parse. You can access your property via :
console.log(user[0].password);
You can't access your property with user.password
because user
variable is not object, it's an array, your object is stored at the zero index of your array.
You already have JSON object
. hence, no need to parse it again.
DEMO
var user = [{ _id: "5902086ecbc0dd11e4870fd9",
password: '$2a$08$FIpkmFT1WDZggQYyBA4CVuop6pelbKBfUEJ1/KAVIV2Si9Ho1EYhi',
email: '[email protected]',
lastName: 'v',
firstName: 'j',
updatedDate: "2017-04-27T15:04:14.483Z",
createdDate: "2017-04-27T15:04:14.483Z" } ];
var password = user[0].password;
console.log(password);
The variable 'user' is not a JSON array. It's an array with a single Javascript object as its element. JSON.parse(arg) can only be used to parse a JSON string to a plain Javascript object. That being said, to access the javascript object within the array, you can do:
var userData = user[0];
To access the password within the variable, userData, you can do:
var password = userData.password;
Log the password to the console with:
console.log(password);
Try This:
var user = [ {_id:'5902086ecbc0dd11e4870fd9',password: '$2a$08$FIpkmFT1WDZggQYyBA4CVuop6pelbKBfUEJ1/KAVIV2Si9Ho1EYhi',email: '[email protected]',lastName: 'v',firstName: 'j',updatedDate: '2017-04-27T15:04:14.483Z',createdDate:' 2017-04-27T15:04:14.483Z' } ];
var obj = user[0];
console.log(obj.password);
本文标签: javascriptHow to parse JSON in node jsStack Overflow
版权声明:本文标题:javascript - How to parse JSON in node js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741522764a2383285.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论