json - 在 AngularJS 中解析 JSON 字符串 - 给出未定义

标签 json angularjs

嗨,我的 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/

相关文章:

java - 如何解析以下JSON

json - 如何在 solr 6 中导入露天元数据和内容

javascript - 我的 javascript 适用于除 IE8 之外的所有浏览器

asp.net - 如何保存搜索,选择最近的搜索,然后使用 jQuery 和/或 asp.net 填充表单,就像 Priceline 所做的那样

c++ - 使用rapidJson压缩json文件后如何将输出存储在c++字符串中

javascript - Angular 在 IE 中抛出 "Error: Invalid argument."

angularjs:ng-message 总是显示

javascript - AngularJS:这段代码有什么问题吗?

javascript - 无法将对象发送到 NodeRED

javascript - 定义 AngularJS 教程中使用的术语 "routing"?