如何在角度2中显示嵌套的JSON?

时间:2017-06-04 10:39:15

标签: json angular

我是角色2的新手。我在html视图中显示嵌套JSON时遇到问题。我跟随json。

$data = $request->all();
$referer =  $data['redirect_to'] ? url($data['redirect_to']) : url('/');
if($allowAccess) return redirect($referer);

我在 GetAllMobile 数组中收到此消息。 我的服务看起来像这样

{
    "totalItems": 347,
    "totalPages": 35,
    "results": [
        {
            "SNR_Title": "Apple - iPhone 6s 128GB - Gold (AT&T)",
            "SNR_Brand": "Apple",
            "SNR_ModelNo": "MKRP2LL/A",
            "SNR_Description": "A 4.7-inch Retina HD display with 3D Touch. 7000 series aluminum and stronger cover glass. An A9 chip with 64-bit desktop-class architecture. All new 12MP iSight camera with Live Photos. Touch ID. Faster LTE and Wi-Fi. Long battery life and iOS 10 and iCloud. All in a smooth, continuous unibody design.",
            "SNR_SKU": "BB5580428",
            "SNR_ImageURL": "https://img.bbystatic.com/BestBuy_US/images/products/5580/5580428_sa.jpg",
            "SNR_ProductURL": "https://api.bestbuy.com/click/-/5580428/pdp",
            "SNR_UPC": "400055804286",
            "SNR_Price": "699.99",
            "SNR_Available": "BESTBUY"
        },
        {
            "SNR_Title": "Apple - iPhone 6s 128GB - Gold (Sprint)",
            "SNR_Brand": "Apple",
            "SNR_ModelNo": "MKRP2LL/A",
            "SNR_Description": "A 4.7-inch Retina HD display with 3D Touch. 7000 series aluminum and stronger cover glass. An A9 chip with 64-bit desktop-class architecture. All new 12MP iSight camera with Live Photos. Touch ID. Faster LTE and Wi-Fi. Long battery life and iOS 10 and iCloud. All in a smooth, continuous unibody design.",
            "SNR_SKU": "BB5580908",
            "SNR_ImageURL": "https://img.bbystatic.com/BestBuy_US/images/products/5580/5580908_sa.jpg",
            "SNR_ProductURL": "https://api.bestbuy.com/click/-/5580908/pdp",
            "SNR_UPC": "400055809083",
            "SNR_Price": "699.99",
            "SNR_Available": "BESTBUY"
        },
        {
            "SNR_Title": "Apple - iPhone 6s 128GB - Gold (Verizon)",
            "SNR_Brand": "Apple",
            "SNR_ModelNo": "MKRP2LL/A",
            "SNR_Description": "A 4.7-inch Retina HD display with 3D Touch. 7000 series aluminum and stronger cover glass. An A9 chip with 64-bit desktop-class architecture. All new 12MP iSight camera with Live Photos. Touch ID. Faster LTE and Wi-Fi. Long battery life and iOS 10 and iCloud. All in a smooth, continuous unibody design.",
            "SNR_SKU": "BB5581618",
            "SNR_ImageURL": "https://img.bbystatic.com/BestBuy_US/images/products/5581/5581618_sa.jpg",
            "SNR_ProductURL": "https://api.bestbuy.com/click/-/5581618/pdp",
            "SNR_UPC": "400055816180",
            "SNR_Price": "699.99",
            "SNR_Available": "BESTBUY"
        },
        {
            "SNR_Title": "Apple - iPhone 6s 128GB - Silver (AT&T)",
            "SNR_Brand": "Apple",
            "SNR_ModelNo": "MKRM2LL/A",
            "SNR_Description": "A 4.7-inch Retina HD display with 3D Touch. 7000 series aluminum and stronger cover glass. An A9 chip with 64-bit desktop-class architecture. All new 12MP iSight camera with Live Photos. Touch ID. Faster LTE and Wi-Fi. Long battery life and iOS 10 and iCloud. All in a smooth, continuous unibody design.",
            "SNR_SKU": "BB5580429",
            "SNR_ImageURL": "https://img.bbystatic.com/BestBuy_US/images/products/5580/5580429_sa.jpg",
            "SNR_ProductURL": "https://api.bestbuy.com/click/-/5580429/pdp",
            "SNR_UPC": "400055804293",
            "SNR_Price": "699.99",
            "SNR_Available": "BESTBUY"
        },
        {
            "SNR_Title": "Apple - iPhone 6s 128GB - Silver (Sprint)",
            "SNR_Brand": "Apple",
            "SNR_ModelNo": "MKRM2LL/A",
            "SNR_Description": "A 4.7-inch Retina HD display with 3D Touch. 7000 series aluminum and stronger cover glass. An A9 chip with 64-bit desktop-class architecture. All new 12MP iSight camera with Live Photos. Touch ID. Faster LTE and Wi-Fi. Long battery life and iOS 10 and iCloud. All in a smooth, continuous unibody design.",
            "SNR_SKU": "BB5580909",
            "SNR_ImageURL": "https://img.bbystatic.com/BestBuy_US/images/products/5580/5580909_sa.jpg",
            "SNR_ProductURL": "https://api.bestbuy.com/click/-/5580909/pdp",
            "SNR_UPC": "400055809090",
            "SNR_Price": "699.99",
            "SNR_Available": "BESTBUY"
        },
        {
            "SNR_Title": "Apple - iPhone 6s 128GB - Silver (Verizon)",
            "SNR_Brand": "Apple",
            "SNR_ModelNo": "MKRM2LL/A",
            "SNR_Description": "A 4.7-inch Retina HD display with 3D Touch. 7000 series aluminum and stronger cover glass. An A9 chip with 64-bit desktop-class architecture. All new 12MP iSight camera with Live Photos. Touch ID. Faster LTE and Wi-Fi. Long battery life and iOS 10 and iCloud. All in a smooth, continuous unibody design.",
            "SNR_SKU": "BB5581619",
            "SNR_ImageURL": "https://img.bbystatic.com/BestBuy_US/images/products/5581/5581619_sa.jpg",
            "SNR_ProductURL": "https://api.bestbuy.com/click/-/5581619/pdp",
            "SNR_UPC": "400055816197",
            "SNR_Price": "699.99",
            "SNR_Available": "BESTBUY"
        },
        {
            "SNR_Title": "Apple - iPhone 6s 128GB - Space Gray (AT&T)",
            "SNR_Brand": "Apple",
            "SNR_ModelNo": "MKRL2LL/A",
            "SNR_Description": "A 4.7-inch Retina HD display with 3D Touch. 7000 series aluminum and stronger cover glass. An A9 chip with 64-bit desktop-class architecture. All new 12MP iSight camera with Live Photos. Touch ID. Faster LTE and Wi-Fi. Long battery life and iOS 10 and iCloud. All in a smooth, continuous unibody design.",
            "SNR_SKU": "BB5580426",
            "SNR_ImageURL": "https://img.bbystatic.com/BestBuy_US/images/products/5580/5580426_sa.jpg",
            "SNR_ProductURL": "https://api.bestbuy.com/click/-/5580426/pdp",
            "SNR_UPC": "400055804262",
            "SNR_Price": "699.99",
            "SNR_Available": "BESTBUY"
        },
        {
            "SNR_Title": "Apple - iPhone 6s 16GB - Space Gray (Sprint)",
            "SNR_Brand": "Apple",
            "SNR_ModelNo": "MKRC2LL/A",
            "SNR_Description": "A 4.7-inch Retina HD display with 3D Touch. 7000 Series aluminum and stronger cover glass. An A9 chip with 64-bit desktop-class architecture. 12MP iSight camera with Live Photos. Touch ID. Faster LTE¹ and Wi-Fi. Long battery life.² And iOS 9 and iCloud. All in a smooth continuous unibody design.",
            "SNR_SKU": "BB5440400",
            "SNR_ImageURL": "https://img.bbystatic.com/BestBuy_US/images/products/5440/5440400_sa.jpg",
            "SNR_ProductURL": "https://api.bestbuy.com/click/-/5440400/pdp",
            "SNR_UPC": "400054404005",
            "SNR_Price": "549.99",
            "SNR_Available": "BESTBUY"
        },
        {
            "SNR_Title": "Apple - iPhone 6s 32GB - Gold (AT&T)",
            "SNR_Brand": "Apple",
            "SNR_ModelNo": "MN1K2LL/A",
            "SNR_Description": "A 4.7-inch Retina HD display with 3D Touch. 7000 series aluminum and stronger cover glass. An A9 chip with 64-bit desktop-class architecture. All new 12MP iSight camera with Live Photos. Touch ID. Faster LTE and Wi-Fi. Long battery life and iOS 10 and iCloud. All in a smooth, continuous unibody design.",
            "SNR_SKU": "BB5580395",
            "SNR_ImageURL": "https://img.bbystatic.com/BestBuy_US/images/products/5580/5580395_sa.jpg",
            "SNR_ProductURL": "https://api.bestbuy.com/click/-/5580395/pdp",
            "SNR_UPC": "400055803951",
            "SNR_Price": "599.99",
            "SNR_Available": "BESTBUY"
        },
        {
            "SNR_Title": "Apple - iPhone 6s 32GB - Gold (Sprint)",
            "SNR_Brand": "Apple",
            "SNR_ModelNo": "MN1K2LL/A",
            "SNR_Description": "A 4.7-inch Retina HD display with 3D Touch. 7000 series aluminum and stronger cover glass. An A9 chip with 64-bit desktop-class architecture. All new 12MP iSight camera with Live Photos. Touch ID. Faster LTE and Wi-Fi. Long battery life and iOS 10 and iCloud. All in a smooth, continuous unibody design.",
            "SNR_SKU": "BB5580737",
            "SNR_ImageURL": "https://img.bbystatic.com/BestBuy_US/images/products/5580/5580737_sa.jpg",
            "SNR_ProductURL": "https://api.bestbuy.com/click/-/5580737/pdp",
            "SNR_UPC": "400055807379",
            "SNR_Price": "599.99",
            "SNR_Available": "BESTBUY"
        }
    ]
}

我试过这个。

this.httpService.getAllMobiles().subscribe(
    data => {
      const myArray = [];


      for (let key in data) {
        myArray.push(data[key]);
        this.GetAllMobile.push(data[key])

      }
      console.log(this.GetAllMobile)
    }, () => this.loading = false
  );

但它没有向我显示简单的 totalItems和Total pages 。并显示我正在尝试的结果。

<div *ngFor="let item of GetAllMobile">
    <h2>
        Total {{item.totalItems}} {{item.totalPages}} AMAD
    </h2>
</div>

它也没有给我看任何东西。

任何人都可以给我提示或代码,我怎样才能在角色2中用打字稿显示它。 任何帮助将受到高度赞赏。

1 个答案:

答案 0 :(得分:0)

如果GetAllMobile是一个数组,那么您发布的JSON是GetAllMobile中的一个对象,可以按照以下方式显示:

<div *ngFor="let item of GetAllMobile">
  <h2>
      Total {{item.totalItems}} {{item.totalPages}} AMAD
  </h2>

  <div *ngFor="let x of item.results">
    <p>
        {{x.SNR_Title}}
    </p>
  </div>
</div>

但是如果你将JSON设置为GetAllMobile,那么它就会变成一个对象,并且值可以直接访问:

<div>
  <h2>
      Total {{GetAllMobileObject.totalItems}} {{GetAllMobileObject.totalPages}} AMAD
  </h2>
</div>
<div *ngFor="let x of GetAllMobileObject.results">
    <p>
        {{x.SNR_Title}}
    </p>
</div>

我已使用您的代码示例为这两种情况创建了此demo。希望这会有所帮助。