CasperJs loads json data from a local file

荒凉一梦 提交于 2019-12-09 08:51:13

问题


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

回答1:


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"



回答2:


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)




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


来源:https://stackoverflow.com/questions/18435748/casperjs-loads-json-data-from-a-local-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!