2015-05-22 85 views
1

你好我收到來自服務器以下響應閱讀使用Javascript /角

{ 
    "Application1": { 
     "status": "SUCCESS", 
     "comment": "A123456 added successfully to Application" 
    }, 
    "Application2": { 
     "status": "SUCCESS", 
     "comment": "B67890 added successfully to Application" 
    } 
} 

我需要根據狀態來顯示一個消息,我們正在使用的角度和JavaScript我通過我無法循環的JSON值和閱讀一樣,任何幫助,將不勝感激

+1

添加顯示如何以及循環的代碼片段很不錯嗎?這樣才能明白你的問題到底是什麼。 – Sagar

回答

1

的simpliest版本,我可以想像:

<script> 
var json = {"Application1":{"status":"SUCCESS","comment":"A123456 added successfully to Application"},"Application2":{"status":"SUCCESS","comment":"B67890 added successfully to Application"}}; 

for(var t in json) 
    console.log(json[t]['status']); 
</script> 
+0

爲我工作簡單和preicisily我正在尋找 – user1767986

+0

通常不會忘記添加hasOwnProperty檢查,同時迭代使用for循環,否則原型鏈也將被檢查:) – user1707873

1

您可以通過解析字符串作爲JSON讀值:

var obj = JSON.parse('{"Application1":{"status":"SUCCESS","comment":"A123456 added successfully to Application"},"Application2":{"status":"SUCCESS","comment":"B67890 added successfully to Application"}}') 

然後你就可以得到訪問值屬性:

obj.Application1.status 
0

首先檢查響應是JSON對象還是字符串。如果是字符串,則將其解析爲JSON。然後你可以循環它。

使用下列功能

isJson(your_response); 

將返回JSON對象或null。

var whatIsIt = function (object) { 
    var stringConstructor = "test".constructor; 
    var arrayConstructor = [].constructor; 
    var objectConstructor = {}.constructor; 

    if (object === null) { 
     return "null"; 
    } 
    else if (object === undefined) { 
     return "undefined"; 
    } 
    else if (object.constructor === stringConstructor) { 
     return "String"; 
    } 
    else if (object.constructor === arrayConstructor) { 
     return "Array"; 
    } 
    else if (object.constructor === objectConstructor) { 
     return "Object"; 
    } 
    else { 
     return "don't know"; 
    } 
}; 

var isJson = function(o1) 
    { 
     // Validate JSON strings 
     var json_object = null; 
     var type = whatIsIt(o1); 
     switch(type) 
     { 
      case "String": 
       try{ 
        json_object = JSON.parse(o1); 
       }catch (e){ 
        return null; 
       } 
       break; 
      case "Object": 
       try { 
        JSON.stringify(o1); 
        json_object = o1; 
       }catch (e) { 
        return null; 
       } 
       break; 
     } 
     return json_object; 
    }; 
0

假設與服務器的通信是在一個單獨的角度服務進行,你需要使用NG重複和NG-IF的指令, 請在這裏找到的jsfiddle: http://jsfiddle.net/2ju3c6tc/1/

var module = angular.module("myModule", []); 
 

 
module.service('serverCommunicator', ['$http', 
 
    function($http) { 
 
    //code to get the data from the server 
 
    //server Data would hold the JSON object after the AJAX req,however, it is assigned manually over here 
 
    this.serverData = { 
 
     "Application1": { 
 
     "status": "SUCCESS", 
 
     "comment": "A123456 added successfully to Application" 
 
     }, 
 
     "Application2": { 
 
     "status": "SUCCESS", 
 
     "comment": "B67890 added successfully to Application" 
 
     } 
 
    }; 
 
    } 
 
]); 
 

 
module.controller('myController', ['$scope', 'serverCommunicator', 
 
    function($scope, serverCommunicator) { 
 
    $scope.serverData = serverCommunicator.serverData; 
 

 
    } 
 
]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script> 
 
<div ng-app="myModule"> 
 
    <div ng-controller="myController as myCont"> 
 
    <div ng-repeat="dataElement in serverData"> 
 
     <div ng-if="dataElement.status=='SUCCESS'"> 
 
     This is success message :{{dataElement.comment}} 
 
     </div> 
 
     <div ng-if="dataElement.status=='FAILURE'"> 
 
     This is failure message {{dataElement.comment}} 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div>

+0

也許,這是從你的要求矯枉過正我看到的答案:) – user1707873