如何在javascript中解析json响应?

时间:2016-03-29 01:50:17

标签: javascript angularjs

打电话给休息api后,我在javascript中收到这些数据。

{
  "error": false,
  "orders": [
    {
      "oid": "764",
      "cid": "423",
      "name": "Akshay jain",
      "address": "infront of gwal magra talab",
      "mobile": "11111111",
      "email": "abcd@gmail.co",
      "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中解析错误和订单并将其保存在变量中?

2 个答案:

答案 0 :(得分:2)

如果此Order.Query是异步的,则必须在回调中设置结果

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

答案 1 :(得分:0)

尝试使用以下代码。

chattr(1)

来源:this question