【问题标题】:How can parse JSON URL data into given AngularJS dxChart variable如何将 JSON URL 数据解析为给定的 AngularJS dxChart 变量
【发布时间】:2016-10-26 05:48:34
【问题描述】:

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

【问题讨论】:

    标签: javascript java angularjs json


    【解决方案1】:

    我认为,

    您作为输入提供的 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);
    }
    

    【讨论】:

      猜你喜欢
      • 2017-09-16
      • 2020-06-16
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      • 1970-01-01
      • 2012-05-22
      • 1970-01-01
      • 2016-09-03
      相关资源
      最近更新 更多