如何在angular2中正确导出嵌套的JSON到CSV?

时间:2017-08-01 04:10:50

标签: arrays json angular csv export

我想在这里实现的是将我的数组JSON数据导出到csv中,我发现的问题是我的维数阵列JSON数据看起来像这样:

[
   {
      "KODE_CLAIM":"CLM/B061/1701/0001",
      "Valid_desc":"Not Valid",
      "Paid":"Not Paid",
      "DETAILS":[
         {
            "Kode_Service":"SVC/B061/1611/0009",
            "Tanggal_Service":"2016-11-12T00:00:00.000Z",
            "Nomor_Nota":"356444",
            "Merk":"MIYAKO",
            "Kode_Produk":"MCM-528 BTK JLH",
            "Nomor_Seri":"34050077185",
            "Kerusakan":"BODY BAWAH AMBLONG",
            "Biaya":0,
            "Transport":0,
            "Valid":"Not Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0004",
            "Tanggal_Service":"2016-12-10T00
:00:00.000Z",
            "Nomor_Nota":"356462",
            "Merk":"RINNAI",
            "Kode_Produk":"RR-50A",
            "Nomor_Seri":"16060148",
            "Kerusakan":"NASI GOSONG                             ",
            "Biaya":50000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0005",
            "Tanggal_Service":"2016-12-13T00:00:00.000Z",
            "Nomor_Nota":"356464",
            "Merk":"SHIMIZU",
            "Kode_Produk":"PS-135E",
            "Nomor_Seri":"JE1270137",
            "Kerusakan":"MATI TOTAL                           
   ",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0006",
            "Tanggal_Service":"2016-12-15T00:00:00.000Z",
            "Nomor_Nota":"356465",
            "Merk":"MIYAKO",
            "Kode_Produk":"KAS1618KB",
            "Nomor_Seri":"060706691",
            "Kerusakan":"TOMBOL TIDAK BISA",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0007",
            "Tanggal_Service":"2016-12-15T00:00:00.000Z",
            "Nomor_Nota":"356469",
            "Merk":"MIYAKO",
            "Kode_Produk":"MCM-528",
            "Nomor_Seri":"34090462375",
            "Kerusakan":"NASI BASI                          
     ",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0008",
            "Tanggal_Service":"2016-12-24T00:00:00.000Z",
            "Nomor_Nota":"356470",
            "Merk":"MIYAKO",
            "Kode_Produk":"WDP-300",
            "Nomor_Seri":"'0611008904",
            "Kerusakan":"AIR TIDAK NAIK",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0009",
            "Tanggal_Service":"2016-12-24T00:00:00.000Z",
            "Nomor_Nota":"356512",
            "Merk":"MIYAKO",
            "Kode_Produk":"MCM-638",
            "Nomor_Seri":"34070057169",
            "Kerusakan":"ADA PERCIKAN API",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0010",
            "Tanggal_Service":"2016-12-26T00:00:00.000Z",
            "Nomor_Nota":"356513",
            "Merk":"MIYAKO",
            "Kode_Produk":"MCM-528 BTK JLH",
            "Nomor_Seri":"34090124901",
            "Kerusakan":"NASI BASI/BAU/BERAIR                    ",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0011",
            "Tanggal_Service":"2016-12-27T00:00:00.000Z",
            "Nomor_Nota":"356514",
            "Merk":"MIYAKO",
            "Kode_Produk":"MCM-612",
            "Nomor_Seri":"34090432943",
            "Kerusakan":"NASI BASI                          
     ",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0012",
            "Tanggal_Service":"2016-12-27T00:00:00.000Z",
            "Nomor_Nota":"356515",
            "Merk":"MIYAKO",
            "Kode_Produk":"MCM-508",
            "Nomor_Seri":"34110174877",
            "Kerusakan":"MATI",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061
/1612/0013",
            "Tanggal_Service":"2016-12-29T00:00:00.000Z",
            "Nomor_Nota":"356516",
            "Merk":"RINNAI",
            "Kode_Produk":"RI-522E",
            "Nomor_Seri":"1605002734B",
            "Kerusakan":"BAU GAS/ BOCOR                          ",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0014",
            "Tanggal_Service":"2016-12-29T00
:00:00.000Z",
            "Nomor_Nota":"356517",
            "Merk":"SHIMIZU",
            "Kode_Produk":"PS-135E",
            "Nomor_Seri":"JCD450661",
            "Kerusakan":"MATI",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         }
      ]
   },
   {
      "KODE_CLAIM":"CLM/B061/1701/0001",
      "Valid_desc":"Valid",
      "Paid":"Not Paid",
      "DETAILS":[
         {
            "Kode_Service":"SVC/B061/1611/0009",
            "Tanggal_Service":"2016-11-12T00:00:00.000Z",
            "Nomor_Nota":"356444",
            "Merk":"MIYAKO",
            "Kode_Produk":"MCM-528 BTK JLH",
            "Nomor_Seri":"34050077185",
            "Kerusakan":"BODY BAWAH AMBLONG",
            "Biaya":0,
            "Transport":0,
            "Valid":"Not Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0004",
            "Tanggal_Service":"2016-12-10T00:00:00.000Z",
            "Nomor_Nota":"356462",
            "Merk":"RINNAI",
            "Kode_Produk":"RR-50A",
            "Nomor_Seri":"16060148",
            "Kerusakan":"NASI GOSONG                            
 ",
            "Biaya":50000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0005",
            "Tanggal_Service":"2016-12-13T00:00:00.000Z",
            "Nomor_Nota":"356464",
            "Merk":"SHIMIZU",
            "Kode_Produk":"PS-135E",
            "Nomor_Seri":"JE1270137",
            "Kerusakan":"MATI TOTAL                              ",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0006",
            "Tanggal_Service":"2016-12-15T00:00:00.000Z",
            "Nomor_Nota":"356465",
            "Merk":"MIYAKO",
            "Kode_Produk":"KAS1618KB",
            "Nomor_Seri":"060706691",
            "Kerusakan":"TOMBOL TIDAK
 BISA",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0007",
            "Tanggal_Service":"2016-12-15T00:00:00.000Z",
            "Nomor_Nota":"356469",
            "Merk":"MIYAKO",
            "Kode_Produk":"MCM-528",
            "Nomor_Seri":"34090462375",
            "Kerusakan":"NASI BASI                               ",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0008",
            "Tanggal_Service":"2016-12-24T00:00:00.000Z",
            "Nomor_Nota":"356470",
            "Merk":"MIYAKO",
            "Kode_Produk":"WDP-300",
            "Nomor_Seri":"'0611008904",
            "Kerusakan":"AIR TIDAK NAIK",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0009",
            "Tanggal_Service":"2016-12-24T00:00:00.000Z",
            "Nomor_Nota":"356512",
            "Merk":"MIYAKO",
            "Kode_Produk":"MCM-638",
            "Nomor_Seri":"34070057169",
            "Kerusakan":"ADA PERCIKAN API",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0010",
            "Tanggal_Service":"2016-12-26T00:00:00.000Z",
            "Nomor_Nota":"356513",
            "Merk":"MIYAKO",
            "Kode_Produk":"MCM-528 BTK JLH",
            "Nomor_Seri":"34090124901",
            "Kerusakan":"NASI BASI/BAU/BERAIR       
             ",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0011",
            "Tanggal_Service":"2016-12-27T00:00:00.000Z",
            "Nomor_Nota":"356514",
            "Merk":"MIYAKO",
            "Kode_Produk":"MCM-612",
            "Nomor_Seri":"34090432943",
            "Kerusakan":"NASI BASI                               ",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0012",
            "Tanggal_Service":"2016-12-27T00:00:00.000Z",
            "Nomor_Nota":"356515",
            "Merk":"MIYAKO",
            "Kode_Produk":"MCM-508",
            "Nomor_Seri":"34110174877",
            "Kerusakan":"MATI",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0013",
            "Tanggal_Service":"2016-12-29T00
:00:00.000Z",
            "Nomor_Nota":"356516",
            "Merk":"RINNAI",
            "Kode_Produk":"RI-522E",
            "Nomor_Seri":"1605002734B",
            "Kerusakan":"BAU GAS/ BOCOR                          ",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         },
         {
            "Kode_Service":"SVC/B061/1612/0014",
            "Tanggal_Service":"2016-12-29T00:00:00.000Z",
            "Nomor_Nota":"356517",
            "Merk":"SHIMIZU",
            "Kode_Produk":"PS-135E",
            "Nomor_Seri":"JCD450661",
            "Kerusakan":"MATI",
            "Biaya":25000,
            "Transport":0,
            "Valid":"Valid"
         }
      ]
   }
]

然后我导出到excel时得到的只是第一个维JSON数组,细节是用OBJECT OBJECT OBJECT编写的,如下所示:

enter image description here

以下是我如何编码,我正在使用Angular2-csv插件,以下是我的代码的样子:

report.component.ts:

import { Angular2Csv } from 'angular2-csv/Angular2-csv';

  public options = { 
      fieldSeparator: ',',
      quoteStrings: '"',
      decimalseparator: '.',
      showLabels: true, 
      showTitle: true,
      useBom: true
    };

  exportExcel(){

    new Angular2Csv(this.claimReport, "report", this.options);
  }

和我的report.component.html:

<button class="btn btn-default btn-with-icon" (click)="exportExcel()"><i class="fa fa-print"></i>Export</button>

然后我尝试打印DETAILS,我应该手动访问该数组,所以这是我如何访问它:

  exportExcel(){

    new Angular2Csv(this.claimReport[0].DETAILS, "report", this.options);
  }

然后它打印细节但只有第一个数组DETAILS,我怎样才能完全打印JSON?

0 个答案:

没有答案