如何在controller.js中读取以下json的值。因为我必须在controller.js中设置值

时间:2017-10-09 13:31:46

标签: angularjs

通过查看以下json数据,让我知道如何访问响应值

登录控制器

app.controller('loginController', function($scope,$http, $location, 
   $rootScope,$window) {
      $scope.submit = function(){
      $rootScope.userName=$scope.username;
      var username=$scope.username;
      var password=$scope.password;
      $http({
             method: 'POST',
             url: 'http://localhost:8082/APIGateway/APILoginService/login',
             data: jsonData,
             headers : {
                 'Content-Type': 'application/json',
                 'Orgkey': 'S2lyYW5fcmFqYWdvcGFs'
             }         
         })
         .then(function successCallback(response) {
         if(response.data.code=="Invalid loginId or password" || response.data.message=="Invalid loginId or password"){
            $scope.errorMessage = 'Invalid LoginId/Password';
         } else {
             $rootScope.loggedIn=true;
             $rootScope.userName = response.data.APIUserData;
             $rootScope.roleName=response.data.roleName;      
         } 
     },         
  };
});

这是json数据。

    {
        "APIUserData": [
            {
                "userID": "deve",
                "firstName": "De",
                "lastName": "Singh",
                "mobileNumber": "990064533",
                "emailID": "xyz@gmail.com",
                "roleID": 1,
                "Org_Id": 1,
                "status": 1
            }
        ]
    }

由于APIUserData,我遇到了问题。如果我删除它,我可以轻松访问该值。任何人都可以帮我解决问题

1 个答案:

答案 0 :(得分:0)

APIUserData是一个数组。你可以这样做:

var myResponse = {
        "APIUserData": [
            {
                "userID": "deve",
                "firstName": "De",
                "lastName": "Singh",
                "mobileNumber": "990064533",
                "emailID": "xyz@gmail.com",
                "roleID": 1,
                "Org_Id": 1,
                "status": 1
            }
        ]
    };

var roleID = myResponse.APIUserData[0].roleID;