如何从具有差异格式的Json文件导入数据?

时间:2017-04-30 11:22:11

标签: angularjs json angular-ui-grid ui-grid

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

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

2 个答案:

答案 0 :(得分:1)

您应该手动初始化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

答案 1 :(得分:0)

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

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

&#13;
&#13;
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"]}

})
&#13;
<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>
&#13;
&#13;
&#13;