简体   繁体   中英

How Can I Get a Json File Array From Java Script

I have a Json file containing arrays for instance:

{
"Test": ["a","aa","aaa"]
}

I want to grab this array through my JavaScript file and use a variable for the json file

If you are in Node.js, then like this:

const fs = require('fs')

const rawJSON = fs.readFileSync('./data.json', 'utf-8')

const data = JSON.parse(rawJSON)

Or, you can do:

const data = require('./data.json')

If you are in the browser, then you need to use your bundler to resolve it in the build step.

Something like webpack may be able to do that just with this code.

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