如何将每个数据从json推送到angularjs

时间:2016-06-06 04:55:59

标签: javascript angularjs json

我试图将每个数据从json推送到新数组,因为想要制作一个图形,但它不起作用。

我的期望是:

f = {“FAKULTAS”:“01 / FKIP”,       “FAKULTAS”: “02 / FMIPA”       “FAKULTAS”:“03 / FISIP”}

g = {“MASA20131”:283133,       “MASA20131”:2419,       “MASA20132”:58719}

这是我的Json文件:

{

"DARIMASA":"20131",

"KEMASA":"20142",

"ISIMASA":

[{"masa":"20131"},{"masa":"20132"},{"masa":"20141"},{"masa":"20142"}],

"DATAFAKULTAS":
[
 {
   "FAKULTAS":"01/FKIP",
   "MASA20131":283133,
   "MASA20132":26746,
   "MASA20141":261238,
   "MASA20142":245722
 },
 {
   "FAKULTAS":"02/FMIPA",
   "MASA20131":2419,
   "MASA20132":29,
   "MASA20141":2706,
   "MASA20142":3207
 },
 {
   "FAKULTAS":"03/FISIP",
   "MASA20131":53312,
   "MASA20132":58719,
   "MASA20141":55047,
   "MASA20142":53745
 }]
}

控制器:

.controller("RegMhsSemesterCtrl", function ($scope, $http, chartBar4) {
        $scope.data = {};
          $http.get('data/RegPerSemester.json')
            .success(function (data) {
                 var axis2 = [],seriesA = [],seriesB = [],seriesC = [],seriesD = [];
                  data.forEach(function(row) {
                    axis2.push(row.FAKULTAS);
                    seriesA.push(row.MASA20131);
                    seriesB.push(row.MASA20132);
                    seriesC.push(row.MASA20141);
                    seriesD.push(row.MASA20142);
                    });
                $scope.data.f=axis2;
                $scope.data.g=seriesA;
                $scope.data.h=seriesB;
                $scope.data.i=seriesC;
                $scope.data.j=seriesD;

                console.log($scope.data);
                var i= chartBar4('chartbar4',$scope.data);

                window.onresize=function ()
                 {
                    i.resize()
                 }

            })
            .error(function (error) {
                $scope.data.error = error;
            });
    })

1 个答案:

答案 0 :(得分:2)

您可以使用以下代码:

.controller("RegMhsSemesterCtrl", function ($scope, $http, chartBar4) {
    $scope.data = {};

   $http
      .get('data/RegPerSemester.json')
      .success(function (data) {

           var DATAFAKULTAS = data.DATAFAKULTAS;
           var axis2 = [],seriesA = [],seriesB = [],seriesC = [],seriesD = [];
           for(var i=0; i<DATAFAKULTAS.length; i++) {
               axis2.push({
                   "FAKULTAS": DATAFAKULTAS[i].FAKULTAS
               });

               seriesA.push({
                 "MASA20131": DATAFAKULTAS[i].MASA20131
               });

               .....................................................
               .....................................................

           }
       });