将json写入Angularjs中的文件

时间:2018-06-19 05:11:03

标签: angularjs json

我正在使用Angularjs中的以下代码片段将json对象写入文件:

  var blob = new Blob([angular.toJson($scope.dataToExport)], {type: 'text/json'});

  if (window.navigator && window.navigator.msSaveOrOpenBlob) {
      window.navigator.msSaveOrOpenBlob(blob, fileName);
  }
  else{
   var e = document.createEvent('MouseEvents'),
       a = document.createElement('a');
       a.download = fileName;
       a.href = window.URL.createObjectURL(blob);
       a.dataset.downloadurl = ['text/json', a.download,        

       a.href].join(':');
       e.initEvent('click', true, false, window,
       0, 0, 0, 0, 0, false, false, false, false, 0, null);
       a.dispatchEvent(e);
  }

但代码是在一行中编写完整的json,如:

{"className":"class12","degree":"B.Tech","RollNo":"123","Grades":    
["a","b","b"]}

我希望json应该以json格式编写,如:

{
"className": "class12",
"degree": "B.Tech",
"RollNo": "123",
"Grades": ["a", "b", "b"]
} 

0 个答案:

没有答案