【发布时间】:2013-12-17 05:15:55
【问题描述】:
我一直在玩 javascript 和 casperjs。我有以下几行代码。
casper.thenOpen('somesite', function() {
console.log('clicked ok, new location is ' + this.getCurrentUrl());
// Get info on all elements matching this CSS selector
var town_selector = 'div tr';
var town_names_info = this.getElementsInfo(town_selector); // an array of object literals
// Pull out the town name text and push into the town_names array
var town_names = [];
for (var i = 0; i < town_names_info.length; i++) {
town_names.push(town_names_info[i].text.trim());}
// Dump the town_names array to screen
utils.dump(town_names);
casper.capture('capture5.png');
});
我的输出是这样的。
[
"Address:\n \n address",
"City:\n \ncity",
"State:\n \nstate",
"Zip:\n \nzip",
]
我怎样才能使它成为 json?像这样。
{
"Address":"address",
"City":"city",
"State":"state",
"Zip":"zip"
}
提前致谢。
【问题讨论】:
标签: javascript json casperjs