繁体   English   中英

如何构造正确的JSON格式

[英]How to construct proper JSON format

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

这是我的片段

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"
            }
        ]
    }
]

先感谢您。

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM