嗨,我的 Controller 中有如下代码
myClientApp.controller('ListCtrl', function ($scope,$http,$cookieStore,$location, $routeParams) {
var data = {
"menus": {
"view": true,
"add": true,
"update": true,
"delete": true
},
"linkInfo": {
"labelColumn": "codeName",
"linkColumn": "lookupKey",
"urlInfo": "reference"
},
"resultList": [
"{\"lookupKey\":2,\"clientKey\":1,\"codeName\":\"Application.AppType\",\"codeValue\":\"ApplicationType2\",\"codeDesc\":\"##\",\"updatedBy\":null,\"internalCodeName\":\"Application.AppType\"}",
"{\"lookupKey\":3,\"clientKey\":1,\"codeName\":\"Application.Class\",\"codeValue\":\"Tier 1\",\"codeDesc\":\"Critical Application requiring immediate response in case of a disruption of Service\",\"updatedBy\":null,\"internalCodeName\":\"Application.Class\"}"
]
};
$scope.result = angular.fromJson(data.resultList);
alert($scope.result[0].codeName);
});
它给了我 未定义 .为什么?
最佳答案
因为 resultList
是一个 JSON 字符串数组,而不是单个 JSON 字符串;您需要指定要解码的 key :
$scope.result = [
angular.fromJson(data.resultList[0]),
angular.fromJson(data.resultList[1])
];
alert($scope.result[0].codeName);
关于json - 在 AngularJS 中解析 JSON 字符串 - 给出未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23106334/