简体   繁体   English

我有一个单独的 .json 文件,我需要遍历该文件或通读该文件并执行一些操作。 在 Javascript 中我究竟该怎么做?

[英]I have a separate .json file, I need to iterate over the file or read through the file and do some stuff. How exactly do I do this in Javascript?

I have a folder with an html, css, json, and js file.我有一个包含 html、css、json 和 js 文件的文件夹。 The js file are a list of person's and what they do. js 文件是人员列表以及他们的工作。 I need to analyze the json file contents using javacript.我需要使用 javacript 分析 json 文件内容。 I know that in C or C++ you can open the file for reading and analyze it, then close it.我知道在 C 或 C++ 你可以打开文件进行阅读和分析,然后关闭它。 So, how do I do this in Javascript?那么,我如何在 Javascript 中执行此操作? And do I need to add the name of the JSON array in the.json file, or should i leave the contents as it is?我是否需要在 .json 文件中添加 JSON 数组的名称,还是应该保留原样的内容?

JSON screenshot image JSON 截图图片

Nothing online have worked thus far.到目前为止,在线上没有任何效果。 Hopefully Stackoverflow can help.希望 Stackoverflow 可以提供帮助。

Let's assume you're using nodeJS, you can import the json file using require .假设您使用的是 nodeJS,您可以使用require导入 json 文件。

After you've imported the json file, you can access the object as it would be a JS object.导入 json 文件后,您可以访问 object,因为它是一个 JS object。

const jsonFile = require("./pathtofile.json")

for(let obj in jsonFile) {
    console.log(obj)
}

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

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