Grunt - read json object from file

前端 未结 1 1541
甜味超标
甜味超标 2021-02-12 22:02

I want to use grunt-hash plugin for renaming my js files. This plugin create a new file containing map of renamed files:

hash: {
    options: {
        mapping:          


        
1条回答
  •  一生所求
    2021-02-12 22:28

    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! :)

    0 讨论(0)
提交回复
热议问题