2016-03-29 142 views
0

呼叫休息後,我得到這個數據在JavaScript中。如何解析JavaScript中的json響應?

{ 
    "error": false, 
    "orders": [ 
    { 
     "oid": "764", 
     "cid": "423", 
     "name": "Akshay jain", 
     "address": "infront of gwal magra talab", 
     "mobile": "11111111", 
     "email": "[email protected]", 
     "odate": "2016-03-28 21:50:45", 
     "status": "NEW ORDER", 
     "payment": "1", 
     "ddate": "2016-03-28", 
     "dtime": "1" 
    }, 
    { 
     "oid": "763", 
     "cid": "438", 
     "name": "Vishwakarma Ji", 
     "address": "Narayan Pura Road", 
     "mobile": "0000000000", 
     "email": null, 
     "odate": "2016-03-28 20:02:06", 
     "status": "Confirmed (Ready for Delivery)", 
     "payment": "1", 
     "ddate": "2016-03-28", 
     "dtime": null 
    } 
    ] 
} 

這是我如何存儲在控制器中。

$scope.result = Order.query(); // where Order is a service in angularjs 

如果我想打印$ scope.result.error它給對象對象不是實際的錯誤值。

如何解析JavaScript錯誤和訂單,並將其保存在一個變量?

+0

'VAR數據= JSON.parse( 'server.response' );' –

回答

2

如果Order.Query是異步,你必須設置的結果在回調

app.controller("OrderIndexCtrl",function($scope,Order) { 
    Order.query(function(data){ 
    if(data.error == false) { 
     $scope.result = data.orders; 
    } 
    else { 
     ... 
    } 
    }); 
}); 
0

請嘗試使用以下代碼。

$scope.result = JSON.parse(Order.query()); 

來源:this question

+0

感謝您的回覆。但是,在JSON.parse之後,它在Object.parse –

+0

處發出錯誤意外的令牌o您能否使用Order.query()服務更新該問題? –