如何将JSON URL数据解析为给定的AngularJS dxChart变量

时间:2016-10-26 05:48:35

标签: javascript java angularjs json

Content.JSON

 [{
status: "Allocated",
count: 45
}, {
status: "Bench",
count: 89
}, {
status: "Mobile",
count: 12
}, {
status: "Project",
count: 1
}, {
status: "SAP",
count: 18
}, {
status: "Testing",
count: 68
}, {
status: "vvvv",
count: 70
}];

下面是我的AngularJS模块,我试图获取JSON url数据,但在控制台中我收到以下错误

dxChart.js:9 E2001 - 无效的数据源。请参阅:http://js.devexpress.com/error/15_2/E2001

angular.module('myApp')
.controller('valueController', ['$scope', '$http', '$window', function ($scope, $http, $window) {       

var mainInfo = null;     

  $http({
    method : "GET",
    url : "/JSON/Content.json",
    dataType: 'json',
  }).then(function mySucces(response) {         

     mainInfo = response.data;

    }, function myError(response) 
    {
      $window.alert(response.statusText);
  });

$scope.chartOptions = {

            dataSource: mainInfo,       

            series: {
                argumentField: "status",
                valueField: "count",
                name: "SIDG Java",
                type: "bar",
                color: '#1899dd'
            }
        };
}]);

1 个答案:

答案 0 :(得分:0)

我想,

您作为输入提供的JSON数据格式错误。您可以通过https://jsonformatter.curiousconcept.com/

等在线网站对其进行验证

不要在JSON末尾使用分号。这是正确的格式:

[{
"status": "Allocated",
"count": 45
}, {
"status": "Bench",
"count": 89
}, {
"status": "Mobile",
"count": 12
}, {
"status": "Project",
"count": 1
}, {
"status": "SAP",
"count": 18
}, {
"status": "Testing",
"count": 68
}, {
"status": "vvvv",
"count": 70
}]

还可以使用try catch块来处理解析异常

try {
  mainInfo = JSON.parse(response);
} catch (err) {
  console.log(err);
}