How to read an external local JSON file in JavaScript?

前端 未结 22 1976
醉酒成梦
醉酒成梦 2020-11-22 02:53

I have saved a JSON file in my local system and created a JavaScript file in order to read the JSON file and print data out. Here is the JSON file:

{"res         


        
22条回答
  •  梦谈多话
    2020-11-22 02:57

    When in Node.js or when using require.js in the browser, you can simply do:

    let json = require('/Users/Documents/workspace/test.json');
    console.log(json, 'the json obj');
    

    Do note: the file is loaded once, subsequent calls will use the cache.

提交回复
热议问题