Grunt - read json object from file

让人想犯罪 __ 提交于 2019-12-03 10:19:47
grunt.file.readJSON('your-file.json')

is probably what you are looking for.

I've set up a little test. I have a simple JSON file 'mapping.json', which contains the following JSON object:

{
  "mapping": [
    {"file": "foo.txt"},
    {"file": "bar.txt"}
  ]
}

In my Gruntfile.js I've written the following simple test task, which reads the first object in the 'mapping'-array:

grunt.registerTask('doStuff', 'do some stuff.', function() {
  mapping = grunt.file.readJSON('mapping.json');
  grunt.log.write(mapping.mapping[0]["file"]).ok();
});

When invoking the Grunt task, the console output will be as follows:

$ grunt doStuff
Running "doStuff" task
foo.txtOK

Done, without errors.

I hope this helps! :)

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!