英文:
Json child of child is being set as undefined in Node JS
问题
详细信息
每当我运行JS文件并尝试console.log(DB.other.Clay)
时,它会打印出undefined。
我正在使用内置的Node.js模块fs
,因为有人告诉我它在处理大型JSON文件时性能更好。当我尝试访问DB.other.Clay.buildings
时,它还会引发错误。
代码
const fs = require('node:fs');
fs.readFile('./conquestDB/res.json', 'utf8', (err, data) => {
if(err){
console.log(err);
return;
}
DB = JSON.parse(data)
console.log(DB.other.Clay)
})
JSON文件(高度压缩,原始文件有超过400,000行)
{
"other": {
"Clay": {
"walls": {...},
"buildings": {
"mine": 838,
"missile_silo": 2,
"anti_missile_wall": 611,
"silo_pad_mk2": 8,
"silo_pad": 8,
"sign": 69,
"storage": 462,
"lab": 299,
"house": 91,
"camp": 594,
"rift": 8,
"walls": 37681,
"fortifier": 115
},
"count": 61983,
"locations": {...}
}
}
}
我不明白为什么我无法访问这些JSON对象,而且我对JavaScript中的JSON非常陌生,所以请详细解释一下。
顺祝,FlyingPig525
英文:
Details
Whenever I run the JS file and try console.log(DB.other.Clay)
it prints undefined.
Im using the built in Node JS module fs
because I have been told it would perform better with large Json files. It also throws an error when I try and access DB.other.Clay.buildings
The code
const fs = require('node:fs');
fs.readFile('./conquestDB/res.json', 'utf8', (err, data) => {
if(err){
console.log(err);
return;
}
DB = JSON.parse(data)
console.log(DB.other.Clay)
})
Json file (highly condensed, the original file has over 400k lines
{
"other": {
"Clay": {
"walls": {...},
"buildings": {
"mine": 838,
"missile_silo": 2,
"anti_missile_wall": 611,
"silo_pad_mk2": 8,
"silo_pad": 8,
"sign": 69,
"storage": 462,
"lab": 299,
"house": 91,
"camp": 594,
"rift": 8,
"walls": 37681,
"fortifier": 115
},
"count": 61983,
"locations": {...}
}
}
}
I do not understand why I am unable to access these Json objects and I am very new to Json in Javascript so please explain it thoroughly.
Best regards, FlyingPig525
答案1
得分: 1
如何调试这个
逐步检查数据的“树形”结构
从这里开始:
const fs = require('node:fs');
fs.readFile('./conquestDB/res.json', 'utf8', (err, data) => {
if(err){
console.log(err);
return;
}
DB = JSON.parse(data)
console.log(Object.keys(DB))
})
确保长键列表中包含“other”
如果是的话,继续下一步:
const fs = require('node:fs');
fs.readFile('./conquestDB/res.json', 'utf8', (err, data) => {
if(err){
console.log(err);
return;
}
DB = JSON.parse(data)
console.log(Object.keys(DB.other))
})
检查该键列表是否包含“Clay”。
我怀疑您的数据中存在一个您没有注意到的额外层级。
英文:
How to debug this
Work your way down the "tree" of data
Start with this:
const fs = require('node:fs');
fs.readFile('./conquestDB/res.json', 'utf8', (err, data) => {
if(err){
console.log(err);
return;
}
DB = JSON.parse(data)
console.log(Object.keys(DB))
})
Make sure that long list of keys contains "other"
If so, then advance to the next step:
const fs = require('node:fs');
fs.readFile('./conquestDB/res.json', 'utf8', (err, data) => {
if(err){
console.log(err);
return;
}
DB = JSON.parse(data)
console.log(Object.keys(DB.other))
})
Check if that list of keys includes "Clay".
I suspect you have an extra layer somewhere, that you haven't noticed.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论