CasperJs loads json data from a local file

2019-03-25 01:46发布

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条回答
干净又极端
2楼-- · 2019-03-25 02:23

I have the following working on CasperJS 1.1-beta1 and PhantomJS 1.9.1

test.json

{
    "test": "hello"
}

test.js

var json = require('test.json');
require('utils').dump(json);
casper.echo(json.test); // "hello"
查看更多
Melony?
3楼-- · 2019-03-25 02:27

Here is a complete sample

var casper = require('casper').create();

var json = require('test.json');
require('utils').dump(json);
casper.echo(json['test']);

casper.exit();
查看更多
霸刀☆藐视天下
4楼-- · 2019-03-25 02:37

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)

查看更多
登录 后发表回答