Angularjs 角度JSON解析
好吧,我被难住了。我有一个角度json GET进程,它按预期的方式将结果拉入,但我似乎无法解析和获取结果的各个值 下面是我得到的字符串结果:Angularjs 角度JSON解析,angularjs,json,Angularjs,Json,好吧,我被难住了。我有一个角度json GET进程,它按预期的方式将结果拉入,但我似乎无法解析和获取结果的各个值 下面是我得到的字符串结果: {"result":[{"sys_id":"f4425c21ec0970407e85e2a57ceff437","number":"INC0035062"}]} 这是我的代码: $http({ method:'GET', url:$scope.url, transformResponse: undefined}).then(function(res
{"result":[{"sys_id":"f4425c21ec0970407e85e2a57ceff437","number":"INC0035062"}]}
这是我的代码:
$http({
method:'GET',
url:$scope.url,
transformResponse: undefined}).then(function(response) {
console.log(response.data);
}).catch(function(response) {
console.error('Error', response.status, response.data);
})
.finally(function() {
console.log("All Done");
});
那么,如何获得sys_id的单个值呢
提前感谢,,
DP
*******编辑:
Sajeetharan,仍然有问题。根据您的示例更改了代码:
console.log(response.data);
$scope.data = response.data.result;
console.log($scope.data);
console.log("Sys id is:"+$scope.data[0].sys_id);
输出:
{"result":[{"sys_id":"f4425c21ec0970407e85e2a57ceff437","number":"INC0035062"}]}
undefined
TypeError: Unable to get property '0' of undefined or null reference
at Anonymous function (******.EvScripts.jsdbx:59:3)
at Anonymous function (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:130:399)
at m.prototype.$eval (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:145:96)
at m.prototype.$digest (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:142:165)
at m.prototype.$apply (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:145:399)
at l (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:97:248)
at K (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:101:373)
at y.onload (https://ajax.googleapis.com/ajax/libs/angularjs/1.5.7/angular.min.js:102:397)
Error undefined undefined
如果要访问系统id,请访问阵列数据的第0个索引
$http.get('test.json').then(function(response){
$scope.data = response.data.result;
});
如果要使用表达式访问系统id
Html格式的
<div ng-controller="ListCtrl">
<h1> Sys id is :{{data[0].sys_id}} </h1>
</div>
这就是最终通过angular.fromJson实现的功能:
//CALL TO REST SERVICES
//---------------------
$http({
method:'GET',
url:$scope.url,
transformResponse: undefined}).then(function(response) {
console.log(response.data);
$scope.data = angular.fromJson(response.data);
console.log($scope.data['result'][0].sys_id);
}).catch(function(response) {
console.error('Error', response.status, response.data);
})
.finally(function() {
console.log("All Done");
});
//---------------------
你的url指向哪里?我猜您的响应不包含application/json content-tyoe头,因此Angular不会自动解析指向我的ServiceNow实例上的incident表的itIt点。我确实将标题设置为:$http.defaults.headers.common.Accept=“application/json”;
Accept
标题供服务器使用,即客户端“告诉”服务器哪些格式是可接受的。服务器应使用适当的内容类型标题进行响应。因此,您设置的标题是不相关的。
//CALL TO REST SERVICES
//---------------------
$http({
method:'GET',
url:$scope.url,
transformResponse: undefined}).then(function(response) {
console.log(response.data);
$scope.data = angular.fromJson(response.data);
console.log($scope.data['result'][0].sys_id);
}).catch(function(response) {
console.error('Error', response.status, response.data);
})
.finally(function() {
console.log("All Done");
});
//---------------------