【问题标题】:Assign a key to each object为每个对象分配一个键
【发布时间】: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.stringifyJSON.parse,但没有成功。

【问题讨论】:

    标签: javascript json casperjs


    【解决方案1】:

    要使用 Javascript 创建 JSON 代码,您应该尝试类似

    var someObjectWhichHappensToBeAstring = "user";    
    var data = {name:someObjectWhichHappensToBeAstring, message:"hello"};
    var JSON = JSON.stringify(data);
    

    可以任意嵌套。 所以在你的情况下,如果 jsonObject 已经是一个 json 对象,你可以简单地这样做:

    var data = {data1:jsonObj1, data2:jsonObj2};
    var JSON = JSON.stringify(data);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-15
    • 2015-03-17
    • 2020-11-10
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    相关资源
    最近更新 更多