如何从 Json 格式不同的文件中导入数据?

how can i import data from a Json file with difference format?

如何将此添加到 ui-grid?我有一个 .Json 格式如下:

0:["date","numberOfTransaction", "price"]
1:["20170207", "3029223", "5294194476028"]
2:["20170208", "2176469", "1479374036275"]
3:["20170209", "2902111", "6971208095034"]
4:["all", "8107803", "13744776607337"]

如果您的 JSON 是一个对象,您需要通过 属性 名称访问它。

 <div ng-app="app" ng-controller="ctrl">
     {{arr['0']}}
 </div>

angular.module("app",[])
.controller("ctrl",function($scope){
$scope.arr = {0:["date","numberOfTransaction", "price"],
1:["20170207", "3029223", "5294194476028"],
2:["20170208", "2176469", "1479374036275"],
3:["20170209", "2902111", "6971208095034"],
4:["all", "8107803", "13744776607337"]}

})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 {{arr['0']}}
</div>

您应该手动初始化 ui-grid 选项。即你应该初始化 columnDefsdata 数组 ui-grid

点赞

  angular.forEach($scope.myData,function(data,j){
    angular.forEach(data,function(d,i){
      if(j ==  0)
          $scope.gridOptions.columnDefs.push({field:d});
     else if(i == 0){
        var date = $scope.gridOptions.columnDefs[0];
        var numberOfTransaction = $scope.gridOptions.columnDefs[1];
        var price = $scope.gridOptions.columnDefs[2];
    $scope.gridOptions.data.push({date:data[0],numberOfTransaction:data[1],price:data[2]});
  }  
  }  
 })
})

Demo