简体   繁体   中英

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. The js file are a list of person's and what they do. I need to analyze the json file contents using javacript. I know that in C or C++ you can open the file for reading and analyze it, then close it. So, how do I do this in 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 screenshot image

Nothing online have worked thus far. Hopefully Stackoverflow can help.

Let's assume you're using nodeJS, you can import the json file using require .

After you've imported the json file, you can access the object as it would be a JS object.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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