简体   繁体   中英

How can I parse AJAX Json data in object form in angularJS

This is my JSON data 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
}];

trying to fetch that JSON file

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) {

     $scope.mainInfo = response.data;

      $window.alert(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'
            }
        };

}]);

In alert I am getting :

Localhost:8080 says:

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

And what i am expecting is

Localhost:8080 says:

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],

It is possible that somehow AngularJS cannot detect JSON in the response data. It could be that the Content-Type header in the response is not application/json ?

You can try replacing dataType: 'json' in your $http config with responseType: 'json' to tell AngularJS to anticipate a JSON response.

I'm getting [object,object][object,object][object,object] in the alert by implementing your code. Please check the JSON once again.

You can parse the data by JSON.parse(response.data) , if you need the [object,object] structure

要显示在警报中,请使用JSON.stringify函数将其转换为json字符串。

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