Angular JS在JSON中显示数组中的数组中的值

时间:2015-07-29 15:44:16

标签: javascript arrays json angularjs

我正在使用的JSON如下。我希望能够显示contentInfo中的url。我可以使用当前的角度代码显示uniquKkey,但我不确定如何为数组中的数组做任何事情。我已经尝试过一些事情,让自己陷入困境,所以任何帮助都会受到高度赞赏。基本上你输入一个项目编号到搜索框点击运行并获得该网址。

由于

JSON

 {
        "products": [
            {
                "uniqueKey": "trn:tesco:product:uuid:2cee5245-23a7-4d51-af6d-f63b3e157492",
                "TPNB": "050020430",
                "TPNC": "254876305",
                "GTIN14": "05000119001467",
                "sellingUnitOfMeasure": "EA",
                "marketingTextInfo": [
                    {
                        "text": "Baked beans in tomato sauce.\n",
                        "source": "Sonetto"
                    }
                ],
                "contentInfo": [
                    {
                        "url": "http://img.tesco.com/Groceries/pi/467/5000119001467/IDShot_90x90.jpg",
                        "default": "Y",
                        "source": "Sonetto",
                        "status": "",
                        "type": ""
                    },
                    {
                        "url": "http://img.tesco.com/Groceries/pi/467/5000119001467/IDShot_60x60.jpg",
                        "default": "N",
                        "source": "Sonetto",
                        "status": "",
                        "type": ""
                    },
                    {
                        "url": "http://img.tesco.com/Groceries/pi/467/5000119001467/IDShot_110x110.jpg",
                        "default": "N",
                        "source": "Sonetto",
                        "status": "",
                        "type": ""
                    },
                    {
                        "url": "http://img.tesco.com/Groceries/pi/467/5000119001467/IDShot_225x225.jpg",
                        "default": "N",
                        "source": "Sonetto",
                        "status": "",
                        "type": ""
                    },
                    {
                        "url": "http://img.tesco.com/Groceries/pi/467/5000119001467/IDShot_77x92.jpg",
                        "default": "N",
                        "source": "Sonetto",
                        "status": "",
                        "type": ""
                    },
                    {
                        "url": "http://img.tesco.com/Groceries/pi/467/5000119001467/IDShot_126x150.jpg",
                        "default": "N",
                        "source": "Sonetto",
                        "status": "",
                        "type": ""
                    },
                    {
                        "url": "http://img.tesco.com/Groceries/pi/467/5000119001467/IDShot_150x300.jpg",
                        "default": "N",
                        "source": "Sonetto",
                        "status": "",
                        "type": ""
                    },
                    {
                        "url": "http://img.tesco.com/Groceries/pi/467/5000119001467/IDShot_45x45.jpg",
                        "default": "N",
                        "source": "Sonetto",
                        "status": "",
                        "type": ""
                    },
                    {
                        "url": "http://img.tesco.com/Groceries/pi/467/5000119001467/IDShot_540x540.jpg",
                        "default": "N",
                        "source": "Sonetto",
                        "status": "",
                        "type": ""
                    }
                ],
                "brandInfo": [
                    {
                        "brand": "TESCO",
                        "source": "RMS"
                    }
                ],
                "statusInfo": [
                    {
                        "status": "Approved",
                        "source": "RMS"
                    }
                ],
                "whatsInBoxInfo": [],
                "prizesAwardsWonInfo": [],
                "commercialHierarchy": {
                    "department": "G13 BAKED BEANS & CAN PASTA",
                    "division": "G AMBIENT DRY GROCERY",
                    "group": "G1 MAIN MEALS",
                    "class": "G13A BAKED BEANS & VARIANTS",
                    "subclass": "G13AABAKED BEANS"
                },
                "GTIN": [],
                "status": "",
                "itemNumber": "050020430",
                "TPNA": "401877209",
                "sellByType": "I",
                "ukEPWIndicator": "N",
                "roiEPWIndicator": "N",
                "tillDescription": "BAKED BEANS",
                "selInfo": [
                    {
                        "lineno": "1",
                        "description": "TESCO BAKED BEAN",
                        "source": "RMS"
                    },
                    {
                        "lineno": "2",
                        "description": "IN TOMATO SAUCE",
                        "source": "RMS"
                    },
                    {
                        "lineno": "3",
                        "description": "220G",
                        "source": "RMS"
                    }
                ],
                "description": "",
                "brand": "",
                "customerFriendlyDescription": "Tesco Baked Beans In Tomato Sauce 220G",
                "components": [],
                "ingredients": [
                    "Haricot beans (49%)",
                    "Water",
                    "Tomato Purèe (20%)",
                    "Sugar",
                    "Modified Maize Starch",
                    "Salt",
                    "Onion Powder",
                    "Colour (Paprika Extract)",
                    "Maltodextrin",
                    "Spice Extracts"
                ],
                "storage": [
                    "Store in a cool, dry place. Once opened transfer contents into a non metallic",
                    "container, cover and refrigerate. Consume within 2 days."
                ],
                "gda": [
                    {
                        "name": "Energy",
                        "value": "190",
                        "percent": "10"
                    },
                    {
                        "name": "Fat",
                        "value": "1.1",
                        "percent": "2"
                    },
                    {
                        "name": "Saturates",
                        "value": "0.2",
                        "percent": "1"
                    },
                    {
                        "name": "Salt",
                        "value": "1.4",
                        "percent": "23"
                    },
                    {
                        "name": "Sugars",
                        "value": "12.1",
                        "percent": "13"
                    }
                ],
                "nutritionalInfo": [
                    {
                        "name": "Typical Values",
                        "perComp": "100g contain",
                        "perServing": "Each can (220g) contains"
                    },
                    {
                        "name": "Energy",
                        "perComp": "365kJ  (87kcal) ",
                        "perServing": "803kJ  (190kcal) "
                    },
                    {
                        "name": "Protein",
                        "perComp": "4.3g",
                        "perServing": "9.5g"
                    },
                    {
                        "name": "Carbohydrate",
                        "perComp": "14.1g",
                        "perServing": "31.0g"
                    },
                    {
                        "name": "Sugars",
                        "perComp": "5.5g",
                        "perServing": "12.1g"
                    },
                    {
                        "name": "Fat",
                        "perComp": "0.5g",
                        "perServing": "1.1g"
                    },
                    {
                        "name": "Saturates",
                        "perComp": "0.1g",
                        "perServing": "0.2g"
                    },
                    {
                        "name": "Fibre",
                        "perComp": "4.1g",
                        "perServing": "9.0g"
                    },
                    {
                        "name": "Salt",
                        "perComp": "0.6g",
                        "perServing": "1.4g"
                    }
                ],
                "allergenInfo": [],
                "defaultImage": "",
                "imageUrl": [],
                "marketingText": "",
                "pkgDimensionInfo": [],
                "productSummaryInfo": [],
                "countryAuthInfo": [
                    "GB",
                    "IE"
                ],
                "suspended": "N",
                "baseDescriptionInfo": [
                    {
                        "lineNo": "1",
                        "description": "TESCO BAKED BEAN",
                        "source": "RMS"
                    },
                    {
                        "lineNo": "2",
                        "description": "IN TOMATO SAUCE ",
                        "source": "RMS"
                    },
                    {
                        "lineNo": "3",
                        "description": "220G            ",
                        "source": "RMS"
                    }
                ],
                "epiSelInfo": [
                    {
                        "lineNo": "1",
                        "description": "",
                        "source": "RMS"
                    },
                    {
                        "lineNo": "2",
                        "description": "",
                        "source": "RMS"
                    },
                    {
                        "lineNo": "3",
                        "description": "",
                        "source": "RMS"
                    },
                    {
                        "lineNo": "4",
                        "description": "",
                        "source": "RMS"
                    },
                    {
                        "lineNo": "5",
                        "description": "",
                        "source": "RMS"
                    }
                ],
                "styleCode": "",
                "unitQty": "KG",
                "epiSelCategory": "",
                "productDatesInfo": [
                    {
                        "country": "GB",
                        "date": "",
                        "dateType": "deactivate",
                        "source": "RMS"
                    },
                    {
                        "country": "IE",
                        "date": "",
                        "dateType": "deactivate",
                        "source": "RMS"
                    },
                    {
                        "country": "GB",
                        "date": "",
                        "dateType": "suspend",
                        "source": "RMS"
                    },
                    {
                        "country": "IE",
                        "date": "",
                        "dateType": "suspend",
                        "source": "RMS"
                    },
                    {
                        "country": "GB",
                        "date": "06/08/1989 00:00:00",
                        "dateType": "launch",
                        "source": "RMS"
                    },
                    {
                        "country": "IE",
                        "date": "22/03/2009 00:00:00",
                        "dateType": "launch",
                        "source": "RMS"
                    }
                ],
                "drainedIndicatorInfo": [
                    {
                        "country": "GB",
                        "indicator": "N",
                        "source": "RMS"
                    },
                    {
                        "country": "IE",
                        "indicator": "N",
                        "source": "RMS"
                    }
                ],
                "quantityInfo": [
                    {
                        "quantity": "220",
                        "quantityUom": "G",
                        "source": "RMS"
                    }
                ],
                "variantReasonCode": "N",
                "commercialHierarchyInfo": {
                    "divisionCode": "G",
                    "divisionNumber": "700",
                    "divisionName": "AMBIENT DRY GROCERY",
                    "departmentCode": "G1",
                    "departmentNumber": "710",
                    "departmentName": "MAIN MEALS",
                    "sectionCode": "G13",
                    "sectionNumber": "713",
                    "sectionName": "BAKED BEANS & CAN PASTA",
                    "classCode": "G13A",
                    "classNumber": "1",
                    "className": "BAKED BEANS & VARIANTS",
                    "subclassCode": "G13AA",
                    "subclassNumber": "1",
                    "subclassName": "BAKED BEANS"
                },
                "href": "http://product.global.tesco.org:8080/v2/products/trn%3Atesco%3Aproduct%3Auuid%3A2cee5245-23a7-4d51-af6d-f63b3e157492"
            }
        ],
        "total": 1,
        "missingSet": []
    }

HTML

    <tr ng-repeat = "oneProduct in products">
                            <td ng-show="EANSwitch"> {{oneProduct.uniqueKey}} </td>
                            <td ng-show="descriptionSwitch"> {{productContentInfo.url}} </td>

Angular JS

    $scope.runSearch = function() {

        $http.get("http://product.global.tesco.org:8080/v2/products?gtin=" + $scope.searchInput).success(function(theData) {

            $scope.products = theData.products;
            $scope.productContentInfo = products.contentInfo
        });

    }

2 个答案:

答案 0 :(得分:0)

您可以使用其他ngRepeat

<tr ng-repeat = "oneProduct in products">
    <td ng-show="EANSwitch"> {{oneProduct.uniqueKey}} </td>
    <td ng-show="descriptionSwitch"> {{productContentInfo.url}} </td>
    <td ng-repeat="content in oneProduct.contentInfo">
        {{content.url}}
    </td>

答案 1 :(得分:0)

我不明白你对$scope.productContentInfo = products.contentInfo的意思。也许您想使用$scope.productContentInfo = $scope.products.contentInfo;。但您可以完全跳过它,因为您已在$scope.products

中获得此信息
  $scope.runSearch = function() {
    $http.get("http://product.global.tesco.org:8080/v2/products?gtin=" + $scope.searchInput).success(function(theData) {
        $scope.products = theData.products;
    });
}

现在,对于HTML,您可以使用另一个ng-repeat而不是contentInfo。 因此,修改后的HTML就是这样的:

    <tr ng-repeat = "oneProduct in products">
      <td ng-show="EANSwitch"> {{oneProduct.uniqueKey}} </td>
      <td ng-show="descriptionSwitch" ng-repeat="u in oneProduct.contentInfo"> {{u.url}} </td>