简体   繁体   English

如何在angularJS中以对象形式解析AJAX Json数据

[英]How can I parse AJAX Json data in object form in angularJS

This is my JSON data Content.json 这是我的JSON数据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 尝试获取该JSON文件

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: Localhost:8080说:

[{
    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: Localhost:8080说:

[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. AngularJS可能无法以某种方式检测到响应数据中的JSON。 It could be that the Content-Type header in the response is not application/json ? 响应中的Content-Type标头可能不是application/json吗?

You can try replacing dataType: 'json' in your $http config with responseType: 'json' to tell AngularJS to anticipate a JSON response. 您可以尝试将$http配置中的dataType: 'json'替换为responseType: 'json'以告诉AngularJS预期JSON响应。

I'm getting [object,object][object,object][object,object] in the alert by implementing your code. 通过实现您的代码,我在警报中得到了[object,object] [object,object] [object,object]。 Please check the JSON once again. 请再次检查JSON。

You can parse the data by JSON.parse(response.data) , if you need the [object,object] structure 如果需要[object,object]结构,则可以通过JSON.parse(response.data)解析数据。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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