简体   繁体   中英

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

Below is my AngularJS Module where I am trying to fetch JSON url data but in console I am getting below error

dxChart.js:9 E2001 - Invalid data source. See: 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'
            }
        };
}]);

I think,

The JSON data which you are providing as input is wrongly formatted. You can validate it from some online sites like https://jsonformatter.curiousconcept.com/

Do not use semicolon at the end of JSON. Here is correct format :

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

Also make use of try catch block to handle parsing exceptions

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM