我正在尝试将以下json文件读入节点js:
myData.json
[
{"1000000": {
"name": "ALEX",
"intern": false,
"purchase": 76
}},
{"2000000": {
"name": "KELVIN",
"purchase": false,
"days": 46
}},
{"3000000": {
"name": "PUTIN",
"intern": false,
"purchase": 9
}}
]
var fs = require('fs');
var obj = JSON.parse(fs.readFileSync('./flatSearch.json', 'utf8'));
console.log("Name is " + obj[0]);
输出:
{ '1000000' : { "name": "ALEX", "intern": false, "purchase": 76 }}
我需要的是: 1)我只需要名称或实习值或购买价值 2)我只需要值1000000
有人可以帮助我如何在节点JS中获取这些值?
答案 0 :(得分:0)
1-a如果您需要名称值:obj[0]['1000000'].name
或obj[0]['1000000']['name']
1-b如果您需要购买价值:obj[0]['1000000'].purchase
或obj[0]['1000000']['purchase']
2-如果你只需要' 1000000'价值:obj[0]['1000000']
但在这些情况下,您需要确保要检索的对象具有密钥名称' 1000000'并有名称和购买属性。
您可以清楚地改进您的JSON文件。
它的一个较轻的JSON示例:
{
"1000000": {
"name": "ALEX",
"intern": false,
"purchase": 76
},
"2000000": {
"name": "KELVIN",
"purchase": false,
"days": 46
},
"3000000": {
"name": "PUTIN",
"intern": false,
"purchase": 9
}
}