gpt4 book ai didi

javascript - 从 http 响应中提取 json 数组

转载 作者:行者123 更新时间:2023-11-30 10:04:15 24 4
gpt4 key购买 nike

我是 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'}});
}]);

相关的 Controller 代码如下所示:

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: 无法读取未定义的属性“ca_inquire_request”

最佳答案

替换

 $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。在控制台之前的代码中,在响应到来之前记录变量。

关于javascript - 从 http 响应中提取 json 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30140926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com