CasperJs loads json data from a local file

前端 未结 3 1985
忘了有多久
忘了有多久 2021-02-14 00:23

Is there any convenient way to load a local JSON file into a variable with CasperJs?

I saw someone suggest to use

$.getJSON(filename, function() ... 
         


        
3条回答
  •  醉酒成梦
    2021-02-14 01:05

    The solution proposed by @hexid worked for me with one change, i added a './' before the file address to denote it is a local file.

    test.json

    {
        "test": "hello"
    }
    

    test.js

    var utils = require('utils');
    var json = require('./test.json');
    
    utils.dump(json);
    utils.dump(json.test); // hello
    utils.dump(json["test"]); // hello
    

    (i would add it as a comment but I'd need 50+ rep to do that)

提交回复
热议问题