导出为CSV并导入到IndexedDB时的额外引号

时间:2016-09-27 13:52:30

标签: angularjs csv lodash indexeddb papaparse

我正在使用ngCSV,loDash和PapaParse导出和导入一个嵌套数组的对象数组。

数组如下所示:

[
    {
        arrival:"15.34.59",
        cancelled:"",
        comments:[{message: "test", commenttime: "15.34", $$hashKey: "object:552"}]
        date:"27/09/2016"
    },
    {
        arrival:"16.32.59",
        cancelled:true,
        comments:[]
        date:"27/09/2016"
    }
]

当我点击以下按钮时:

<button 
    ng-csv="commenttoJson(filteredRecords)" 
    csv-header="['date', 'comments', 'arrival', 'cancelled']" 
    filename="{{ createCsvFilename(dates) }}">
        Download
</button>

它会触发此功能并下载文件:

$scope.commenttoJson = function(filteredRecords){
    return _.map(filteredRecords, function(record){
        return _.extend({}, record, {comments: JSON.stringify(record.comments)});
    }); 
}

生成的文件如下所示:

date,comments,arrival,cancelled
27/09/2016,"[{""message"":""testing"",""commenttime"":""14.52"",""$$hashKey"":""object:50""}]",14.52.29,,
27/09/2016,[],,TRUE

然后我继续使用这些函数导入它:

$scope.onFileSelect = function ($files) {
  Papa.parse($files[0], {
    header: true,
    skipEmptyLines: true,
    complete: function(results, $files,err) {
      if (err) {
          console.log('err ', err);
      }
      $scope.parseddata = results.data;
    }
  });
};

$scope.importData = function () {
  $indexedDB.openStore('records', function(store){
    store.insert($scope.parseddata).then(function(e){
      store.getAll().then(function(record) {  
        $scope.recordlist = record;
        console.log($scope.recordlist);
        $('.import-data').modal('hide');
        $scope.deleted = false;
      });
    });
  });
};

问题在于它完全导入所有内容,除了评论之外,现在看起来如下所示,并且显然不会被前端正确阅读:

[
    {
        arrival:"15.34.59",
        cancelled:"",
        comments:"[{"message":"test","commenttime":"15.34","$$hashKey":"object:552"}]"
        date:"27/09/2016"
    }, (...)

显然在这个过程中我遗漏了一些东西,因为所有额外的引号都会弄乱我的代码。

关于如何解决这个问题的任何想法?

编辑和修复

$scope.onFileSelect = function ($files) {
  Papa.parse($files[0], {
    header: true,
    skipEmptyLines: true,
    complete: function(results, $files,err) {
        if (err) {
            console.log('err ', err);
        }

    $scope.filteredRecordsToImport = _.map(results.data, function(item){
        return _.extend(item, {comments: item.comments ? JSON.parse(item.comments) : []});
    });
    console.log($scope.filteredRecordsToImport);

    }
  });
};

显然我不得不重新解析JSON以反转stringify并摆脱数组中的额外引号!

1 个答案:

答案 0 :(得分:1)

$scope.onFileSelect = function ($files) {
  Papa.parse($files[0], {
    header: true,
    skipEmptyLines: true,
    complete: function(results, $files,err) {
        if (err) {
            console.log('err ', err);
        }

    $scope.filteredRecordsToImport = _.map(results.data, function(item){
        return _.extend(item, {comments: item.comments ? JSON.parse(item.comments) : []});
    });
    console.log($scope.filteredRecordsToImport);

    }
  });
};

显然我不得不重新解析JSON以反转stringify并摆脱数组中的额外引号!