![](/img/trans.png)
[英]How to automatically parse spring form data to json object? (Spring, jQuery, AJAX, JSON)
[英]How can I parse AJAX Json data in object form in angularJS
这是我的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
}];
尝试获取该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'
}
};
}]);
在警报中,我得到:
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
}];
我期望的是
Localhost:8080说:
[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],[object Object],
AngularJS可能无法以某种方式检测到响应数据中的JSON。 响应中的Content-Type
标头可能不是application/json
吗?
您可以尝试将$http
配置中的dataType: 'json'
替换为responseType: 'json'
以告诉AngularJS预期JSON响应。
通过实现您的代码,我在警报中得到了[object,object] [object,object] [object,object]。 请再次检查JSON。
如果需要[object,object]结构,则可以通过JSON.parse(response.data)解析数据。
要显示在警报中,请使用JSON.stringify函数将其转换为json字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.