【问题标题】:How to construct proper JSON format如何构造正确的 JSON 格式
【发布时间】:2014-01-22 21:14:41
【问题描述】:

我有以下与 casperjs 一起使用的 javascript 代码来迭代一些链接并以 json 格式返回一些数据。

这是我的sn-p

casper.each(links, function (self, link) {

this.thenOpen(link, function () {
    //get work order info
    var town_selector = 'div tr';
    var town_names_info = this.getElementsInfo(town_selector);
    var town_names = [];
    for (var i = 0; i < town_names_info.length; i++) {
        town_names.push(town_names_info[i].text.trim().replace(/\n\s+\n/, ''));
    }
    var jsonTest = arrayToObject(town_names);
    json.push(JSON.stringify(jsonTest));
    casper.capture('./images/workOrder' + workOrder + '.png');
    workOrder++
    utils.dump(jsonTest);
    array.push(jsonTest);
    casper.thenClick(x('/html/body/table[2]/tbody/tr/td[2]/a[2]'), function () {
        //more some stuff here 
        someLinks = this.evaluate(getLinks);

        for (var i = 0; i < someLinks.length; i++) {
            someLinks[i] = "https://somelink" + someLinks[i];
        }
        casper.each(someLinks, function (self, link) {
            self.thenOpen(link, function () {
                var selector = 'div tr';
                var names_info = this.getElementsInfo(town_selector);
                var names = [];
                for (var i = 0; i < names_info.length; i++) {
                    names.push(names_info[i].text.trim().replace(/\n\s+\n/, ''));
                }
                var jsonTest = arrayToObject(names);
                json.push(JSON.stringify(jsonTest));
                utils.dump(jsonTest);
                array.push(jsonTest);
                fs.write('results.json', JSON.stringify(array), 'w');
                casper.capture('./images/lineItem' + lineItem + '.png');
                lineItem++
            });
         });
      });
   });
});

以下是两个活动的输出,每个活动有两个行项目。

    [{"Activity #":"some activity",
      "Customer":"some customer",
      "Account #":"some account"},
     {
      "Line #":"1",
      "Action Required":"",
      "Status":"some status",
      "Product Line":"some product line",
      "Product":"some product"},
     {
      "Line #":"2",
      "Action Required":"",
      "Status":"some status",
      "Product Line":"some product line",
      "Product":"some product"},
     {
      "Activity #":"some other activity",
      "Customer":"some other customer",
      "Account #":"some other account"},
     {
      "Line #":"1",
      "Action Required":"",
      "Status":"some status",
      "Product Line":"some product line",
      "Product":"some product"},
     {
      "Line #":"2",
      "Action Required":"",
      "Status":"some status",
      "Product Line":"some product line",
      "Product":"some product"}]

有人可以帮我把我的输出改成这样吗?

    [{
        "Activity #": "some activity",
        "Customer": "some customer",
        "Account #": "some account",
        "lineItems": [
            {
                "Line #": "1",
                "Action Required": "",
                "Status": "some status",
                "Product Line": "some product line",
                "Product": "some product"
            },
            {
                "Line #": "2",
                "Action Required": "",
                "Status": "some status",
                "Product Line": "some product line",
                "Product": "some product"
            }
        ]
    },
    {
        "Activity #": "some activity",
        "Customer": "some customer",
        "Account #": "some account",
        "lineItems": [
            {
                "Line #": "1",
                "Action Required": "",
                "Status": "some status",
                "Product Line": "some product line",
                "Product": "some product"
            },
            {
                "Line #": "2",
                "Action Required": "",
                "Status": "some status",
                "Product Line": "some product line",
                "Product": "some product"
            }
        ]
    }
]

提前谢谢你。

【问题讨论】:

  • WTH 是 jsonjsonTest?
  • 对不起。也许这会有所帮助。 //arrayToObject function arrayToObject(arr) { var out = {}; arr.forEach(function (element) { var keyvalue = element; var key = keyvalue.substring(0, element.indexOf(':')); var value = keyvalue.substring(key.length + 1).trim(); out[key] = value; }); return out; }
  • 好的,但是你用它们做什么?
  • 我把我用 casper 得到的日期推给他们。
  • 然后呢?我没有看到您的代码中再次使用了json

标签: javascript json casperjs


【解决方案1】:

JSON.stringify 使用适当的参数应该可以做到:

fs.write('results.json', JSON.stringify(array, null, 4), 'w');

【讨论】:

  • 我的输出和你的方法一样。
猜你喜欢
  • 2017-07-13
  • 1970-01-01
  • 1970-01-01
  • 2016-03-25
  • 1970-01-01
  • 2016-07-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多