【发布时间】:2016-02-15 21:58:22
【问题描述】:
以下代码 sn-p 正在从网页中提取数据。
jsonObj = {};
jsonObj.title = this.fetchText('a.profile-full-name');
jsonObj.services = this.getHTML('div.info-list-text span:nth-child(2) span');
jsonObj.services = jsonObj.services.replace(/&/g,"and");
jsonObj.location = this.getHTML('div.pro-info-horizontal-list div.info-list-label:nth-child(3) div.info-list-text span');
jsonObj.contact = this.fetchText('span.pro-contact-text');
jsonObj.description = this.getHTML('div.profile-about div:nth-child(1)');
//jsonObj.description.replace(/\s/g, '');
//require('utils').dump(jsonObj);
jsonObj.description = jsonObj.description.replace(/[\t\n]/g,"");
//jsonObj = JSON.stringify(jsonObj, null, '\t');
require('utils').dump(jsonObj);
它的输出是,
{
"title": "Marcelle Guilbeau, Interior Designer",
"services": "Interior Designers & Decorators",
"location": "5007 Wyoming Ave.",
"description": "Marcelle takes her clients on a journey, drawing out their needs to create an oasis that reflects their personal sense of style and renews their connection."
}
{
"title": "Eric Ross Interiors, LLC",
"services": "Interior Designers & Decorators",
"location": "220 Lewisburg Avenue",
"description": "Eric Ross Interiors exists to create beautiful interiors and a luxury design experience for its clients. We are committed to creating whole room environments for our clients in Nashville, Middle Tennessee and Beyond. Our job is to listen to you about the look and feel you want to achieve. We want to make you comfortable Houzz 2013, 2014, 2015 Outstanding Customer Service and 2015 Best of Design"
}
有没有办法为上述每个对象分配一个键。例如,
"data1": {
"title": "Marcelle Guilbeau, Interior Designer",
"services": "Interior Designers & Decorators",
"location": "5007 Wyoming Ave.",
"description": "Marcelle takes her clients on a journey, drawing out their needs to create an oasis that reflects their personal sense of style and renews their connection."
}
"data2" :{
"title": "Eric Ross Interiors, LLC",
"services": "Interior Designers & Decorators",
"location": "220 Lewisburg Avenue",
"description": "Eric Ross Interiors exists to create beautiful interiors and a luxury design experience for its clients. We are committed to creating whole room environments for our clients in Nashville, Middle Tennessee and Beyond. Our job is to listen to you about the look and feel you want to achieve. We want to make you comfortable Houzz 2013, 2014, 2015 Outstanding Customer Service and 2015 Best of Design"
}
我的最终目标是将这些单独的对象转换为 json 数据。我尝试过使用JSON.stringify 和JSON.parse,但没有成功。
【问题讨论】:
标签: javascript json casperjs