2016-10-26 43 views
0

Content.JSON如何JSON解析URL数据为特定的AngularJS dxChart变量

[{ 
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 
}]; 

下面就是我试图获取JSON URL数据,但在控制台中我得到以下错误

我AngularJS模块

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' 
      } 
     }; 
}]); 

回答

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); 
}