从 http 响应中提取 json 数组

Extracting json array from http response

我是 AngularJS 项目的新手,我正在尝试从 http 响应中提取 json 数组以在列表中使用。

响应如下所示:

{
  "DFH0XCMNOperationResponse": {
    "ca_return_code": 0,
    "ca_inquire_request": {
      "ca_last_item_ref": 150,
      "ca_item_count": 15,
      "ca_cat_item": [
        {
          "ca_cost": "002.90",
          "in_stock": 119,
          "ca_description": "Ball Pens Black 24pk",
          "on_order": 0,
          "ca_department": 10,
          "ca_item_ref": 10
        },
        {
          "ca_cost": "002.90",
          "in_stock": 6,
          "ca_description": "Ball Pens Blue 24pk",
          "on_order": 50,
          "ca_department": 10,
          "ca_item_ref": 20
        }
      ],
      "ca_list_start_ref": 0
    },
    "ca_response_message": "+15 ITEMS RETURNED",
    "ca_request_id": "01INQC"
  }
}

资源和请求的代码如下所示:

.factory('getCatalog', ['$resource', function($resource){
    return $resource('catalogmanagertest/v1/apps/bca45894-92f7-49dc-ae54-b23b89ab6c73/catalog', {}, {query: {method:'POST'}});
}]);

相关的控制器代码如下所示:

angular
.module('catalogController', ['ngMaterial', 'ngResource'])
.controller('catalogController', ['$scope', 'getCatalog', 'catalog', function($scope, getCatalog, catalog) {
        $scope.debug = getCatalog.query(); // set scope catalog to array from zOS
        $scope.catalog = catalog.ca_cat_item;
        $scope.message = "This is a test order message";
        this.tiles = buildGridModel({
            icon : "avatar:svg-",
            title: "",
            cost: "€",
            background: "",
            stock: ""
        });
        function buildGridModel(tileTmpl){
            var it, results = [ ];

            var tmp = $scope.debug.DFH0XCMNOperationResponse.ca_inquire_request.ca_cat_item;
            console.log(tmp);

倒数第二行是我遇到的问题。如何提取预期的数组?当我执行 console.log:

时出现新手错误

TypeError: Cannot read property 'ca_inquire_request' of undefined

替换

 $scope.debug = getCatalog.query();

  getCatalog.query().$promise.then(function (result) {
       $scope.debug = result;
       console.log($scope.debug.DFH0XCMNOperationResponse.ca_inquire_request.ca_cat_item);
    });

在这里您可以看到我刚刚将 api 调用转换为 promise.In 您的代码,然后控制台在响应到来之前记录变量。