CasperJs从本地文件加载json数据

时间:2021-08-04 17:49:05

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

用CasperJs将本地JSON文件加载到变量中有什么方便的方法吗?

I saw someone suggest to use

我看到有人建议使用。

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

3 个解决方案

#1


14  

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

我有以下关于CasperJS 1.1-beta1和PhantomJS 1.9.1的工作

test.json

test.json

{
    "test": "hello"
}

test.js

. js

var json = require('test.json');
require('utils').dump(json);
casper.echo(json.test); // "hello"

#2


6  

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.

@hexid提出的解决方案,我只做了一个改动,就增加了a '。在文件地址之前表示它是一个本地文件。

test.json

test.json

{
    "test": "hello"
}

test.js

. 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)

(我想加上一句,但我需要50多名代表才能做到)

#3


3  

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();

#1


14  

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

我有以下关于CasperJS 1.1-beta1和PhantomJS 1.9.1的工作

test.json

test.json

{
    "test": "hello"
}

test.js

. js

var json = require('test.json');
require('utils').dump(json);
casper.echo(json.test); // "hello"

#2


6  

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.

@hexid提出的解决方案,我只做了一个改动,就增加了a '。在文件地址之前表示它是一个本地文件。

test.json

test.json

{
    "test": "hello"
}

test.js

. 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)

(我想加上一句,但我需要50多名代表才能做到)

#3


3  

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();