CasperJs从本地文件加载json数据

mar*_*ant 12 json load file casperjs

有没有方便的方法将本地JSON文件加载到CasperJs的变量中?

我看到有人建议使用

$.getJSON(filename, function() ... 
Run Code Online (Sandbox Code Playgroud)

hex*_*xid 14

我有以下工作在CasperJS 1.1-beta1和PhantomJS 1.9.1

test.json

{
    "test": "hello"
}
Run Code Online (Sandbox Code Playgroud)

test.js

var json = require('test.json');
require('utils').dump(json);
casper.echo(json.test); // "hello"
Run Code Online (Sandbox Code Playgroud)


Vin*_*cia 6

@hexid提出的解决方案为我工作了一次更改,我在文件地址前添加了一个'./'来表示它是一个本地文件.

test.json

{
    "test": "hello"
}
Run Code Online (Sandbox Code Playgroud)

test.js

var utils = require('utils');
var json = require('./test.json');

utils.dump(json);
utils.dump(json.test); // hello
utils.dump(json["test"]); // hello
Run Code Online (Sandbox Code Playgroud)

(我会将其添加为评论,但我需要50多名代表才能这样做)