[英]How can parse JSON URL data into given AngularJS dxChart variable
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'
}
};
}]);
我認為,
您提供作為輸入的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);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.