Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing JSON String in AngularJS - Gives undefined

Tags:

json

angularjs

Hi I have a code like following in my 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);
});

And It gives me undefined . Why?

like image 733
iCode Avatar asked Dec 02 '25 00:12

iCode


1 Answers

Because resultList is an array of JSON strings, not a single JSON string; you need to specify which key you want to decode:

$scope.result = [
    angular.fromJson(data.resultList[0]),
    angular.fromJson(data.resultList[1])
];
alert($scope.result[0].codeName);
like image 194
mingos Avatar answered Dec 03 '25 16:12

mingos