简体   繁体   English

如何获取 JSON 文件来显示子数组?

[英]How do I get a JSON file to show sub-arrays?

I know it's a simple question, but I've tried multiple ways and I can't get it to work.我知道这是一个简单的问题,但我尝试了多种方法,但无法让它发挥作用。 I have a huge JSON file (that I use a reduced version of) which I finally managed to let my script retrieve the file using the require();我有一个巨大的 JSON 文件(我使用了它的简化版本),我终于设法让我的脚本使用require(); function: function:

let wordBank = './Wordbank.json';

function getData() {
  console.log(require(wordBank));
}
getData();

The terminal displays the few lines of my reduced JSON file like this, why?: Notice the green "Array"?终端像这样显示我缩小的 JSON 文件的几行,为什么?:注意绿色的“Array”?

I haven't really been able to find any answer...我真的找不到任何答案......

Use console.dir instead of console.log使用 console.dir 而不是 console.log

console.dir(data, {depth:100})

Its having a depth parameter to expand json upto given level它有一个深度参数来扩展 json 到给定的水平

You need to use JSON.stringify to convert the object to JSON string.您需要使用JSON.stringify将 object 转换为 JSON 字符串。 So you need to change your code like this:所以你需要像这样改变你的代码:

let wordBank = './Wordbank.json';
function getData() {
   console.log(JSON.stringify(require(wordBank)));
}
getData();

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

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