如何使用 *ngFor 显示 json 对象并访问对象的键、值

Posted

技术标签:

【中文标题】如何使用 *ngFor 显示 json 对象并访问对象的键、值【英文标题】:How to display json object using *ngFor and access key,value of object 【发布时间】:2017-03-21 16:46:30 【问题描述】:

我想在ionic 2 中显示来自服务器的以下数据。 并使用*ngFor访问对象的keyvalue

我想使用*ngFor 动态显示这个对象。如何获得?

对于这个问题是否有更简单的解决方案,因为我觉得使用Angular2的开发人员显示这样的json数据真的很常见。


    "status": "success",
    "products": 
        "Admiranda": [
            
                "entity_id": "448",
                "sku": "587",
                "name": "# Adm Ape Maia EDT 50 ml 75009 (9)",
                "image_url": "/path/media/catalog/product/cache/5/8/587.png",
                "url_key": "/path/adm-ape-maia-edt-50-ml-75009-9",
                "price": "15,000",
                "symbol": "LBP ",
                "qty": 0,
                "rating": 0,
                "wishlist": false,
                "specialprice": "7,500",
                "brand": "Admiranda"
            ,
            
                "entity_id": "435",
                "sku": "571",
                "name": "# Adm Aveng Liquid Soap 300ml 73680(3)",
                "image_url": "/path/media/catalog/product/cache/5/7/571.png",
                "url_key": "/path/adm-aveng-liquid-soap-300ml-73680-3",
                "price": "5,000",
                "symbol": "LBP ",
                "qty": 0,
                "rating": 0,
                "wishlist": false,
                "specialprice": "2,500",
                "brand": "Admiranda"
            
        ],
        "Weider": [
            
                "entity_id": "306",
                "sku": "386",
                "name": "32 % Whey Wafer bar choco 35 g",
                "image_url": "/path/media/catalog/product/cache/3/2/32_whey_wafer.png",
                "url_key": "/path/32-whey-wafer-bar-choco-35-g",
                "price": "5,910",
                "symbol": "LBP ",
                "qty": 0,
                "rating": 0,
                "wishlist": false,
                "specialprice": "3,842",
                "brand": "Weider"
            ,
            
                "entity_id": "337",
                "sku": "650",
                "name": "52% Protein Bar Cherry-Yog 50g",
                "image_url": "/path/media/catalog/product/cache/5/2/52_protein_2.png",
                "url_key": "/path/52-protein-bar-cherry-yog-50g",
                "price": "6,819",
                "symbol": "LBP ",
                "qty": 0,
                "rating": 0,
                "wishlist": false,
                "specialprice": "4,432",
                "brand": "Weider"
            
        ],
        "Yoomi": [
            
                "entity_id": "303",
                "sku": "638",
                "name": "5oz feeding bottle+slow flow teat (2 of)",
                "image_url": "/path/media/catalog/product/cache/6/3/638.png",
                "url_key": "/path/5oz-feeding-bottle-slow-flow-teat-2-of",
                "price": "30,909",
                "symbol": "LBP ",
                "qty": 0,
                "rating": 0,
                "wishlist": false,
                "specialprice": "21,636",
                "brand": "Yoomi"
            
        ],
        "Cosmetic products": [
            
                "entity_id": "519",
                "sku": "736",
                "name": "Bebble Body cream",
                "image_url": "/path/media/catalog/product/cache/b/e/bebble_body_cream_175_ml.736_1.png",
                "url_key": "/path/bebble-body-cream",
                "price": "20,909",
                "symbol": "LBP ",
                "qty": 0,
                "rating": 0,
                "wishlist": false,
                "specialprice": "10,455",
                "brand": "Cosmetic products"
            ,
            
                "entity_id": "517",
                "sku": "734",
                "name": "Bebble Body milk",
                "image_url": "/path/media/catalog/product/cache/b/e/bebble_body_milk_200_ml.734.png",
                "url_key": "/path/bebble-body-milk",
                "price": "18,182",
                "symbol": "LBP ",
                "qty": 0,
                "rating": 0,
                "wishlist": false,
                "specialprice": "9,091",
                "brand": "Cosmetic products"
            
        ],
        "Dialfa": [
            
                "entity_id": "483",
                "sku": "184",
                "name": "Dialfa HK LipStick Amber 1955 (3)",
                "image_url": "/path/media/catalog/product/cache/4/_/4_1.png",
                "url_key": "/path/dialfa-hk-lipstick-amber-1955-3",
                "price": "4,500",
                "symbol": "LBP ",
                "qty": 0,
                "rating": 0,
                "wishlist": false,
                "specialprice": "2,250",
                "brand": "Dialfa"
            ,
            
                "entity_id": "495",
                "sku": "323",
                "name": "Dlf  Lip Balm Cindrella 0859 (3)",
                "image_url": "/path/media/catalog/product/cache/1/2/12.png",
                "url_key": "/path/dlf-lip-balm-cindrella-0859-3",
                "price": "4,500",
                "symbol": "LBP ",
                "qty": 0,
                "rating": 0,
                "wishlist": false,
                "specialprice": "2,250",
                "brand": "Dialfa"
            ,
            
                "entity_id": "503",
                "sku": "368",
                "name": "DLF Baby Kitty Sham&Dush250ml0552(8)",
                "image_url": "/path/media/catalog/product/cache/h/k/hk_368.png",
                "url_key": "/path/dlf-baby-kitty-sham-dush250ml0552-8",
                "price": "6,000",
                "symbol": "LBP ",
                "qty": 0,
                "rating": 0,
                "wishlist": false,
                "specialprice": "3,000",
                "brand": "Dialfa"
            ,
            
                "entity_id": "506",
                "sku": "397",
                "name": "DLF Bagno Guanto Sponge WTP 0044 (5)",
                "image_url": "/path/media/catalog/product/cache/3/9/397.png",
                "url_key": "/path/dlf-bagno-guanto-sponge-wtp-0044-5",
                "price": "8,000",
                "symbol": "LBP ",
                "qty": 0,
                "rating": 0,
                "wishlist": false,
                "specialprice": "4,000",
                "brand": "Dialfa"
            
        ],
        "Nevella": [
            
                "entity_id": "277",
                "sku": "118",
                "name": "Nevella Probiotics 100 Tabs",
                "image_url": "/path/media/catalog/product/cache/n/e/nevella2.png",
                "url_key": "/path/nevella-probiotics-100-tabs",
                "price": "5,139",
                "symbol": "LBP ",
                "qty": 0,
                "rating": 0,
                "wishlist": false,
                "specialprice": "4,111",
                "brand": "Nevella"
            ,
            
                "entity_id": "280",
                "sku": "159",
                "name": "Nevella Probiotics 2,000 Sachets",
                "image_url": "/path/media/catalog/product/cache/u/n/untitled_1.png",
                "url_key": "/path/nevella-probiotics-2-000-sachets",
                "price": "103,637",
                "symbol": "LBP ",
                "qty": 0,
                "rating": 0,
                "wishlist": false,
                "specialprice": "103,637",
                "brand": "Nevella"
            
        ],
        "Medi Baby": [
            
                "entity_id": "549",
                "sku": "778",
                "name": "Wet Wipes Medi Baby 100 pcs",
                "image_url": "/path/media/catalog/product/cache/7/7/778.png",
                "url_key": "/path/wet-wipes-medi-baby-100-pcs",
                "price": "3,500",
                "symbol": "LBP ",
                "qty": 0,
                "rating": 0,
                "wishlist": false,
                "specialprice": "2,625",
                "brand": "Medi Baby"
            
        ]
    

【问题讨论】:

听起来像***.com/questions/38554562/… 【参考方案1】:

Angular 默认有 keyvalue 管道用于循环 json 数组。

看看下面的例子:

<select>
    <option *ngFor="let item of json | keyvalue" value=" item.key ">
         item.value 
    </option>
</select>

【讨论】:

【参考方案2】:

*ngfor循环中打印Admiranda数组,首先写一个管道

Pipe.ts

@Pipe(
  name: 'objectValues'
)
export class ObjectValuesPipe implements PipeTransform 
  transform(obj: any) 
    let result = [];
    for (var key in obj) 
      if (obj.hasOwnProperty(key)) 
        result.push(obj[key]);
      
    
    return result;
  

别忘了将你的管道导入@NgModule,这样你就可以像这样使用这个管道了。

<ul *ngFor="let item of items">
   <li *ngFor="let value of item | objectValues">
      value 
   </li>
 </ul>

【讨论】:

感谢您的回复,但有更多详细信息可以在ionic 2 中使用管道吗? ionic 2 在引擎盖下使用 angular 2,我认为您可以将这个管道用作 angular 2 管道。只需将您的管道包含在 app.module.ts 文件中,您就可以在项目管道符号中的任何位置使用该管道。我不知道 ionic 但我确实知道 angular 2。 在我的模板中使用此管道时出现错误Uncaught Error: Template parse errors 有什么建议吗? 通过去掉ul*ngFor之间的空格来解决。 但是我不工作我收到了这个错误Cannot find a differ supporting object '[object Object]' of type 'object'. NgFor only supports binding to Iterables such as Arrays. 提前感谢。

以上是关于如何使用 *ngFor 显示 json 对象并访问对象的键、值的主要内容,如果未能解决你的问题,请参考以下文章

使用 *ngFor 检索和显示 json 对象

在 Angular 2 中使用 *ngFor 和 JSON 对象

Angular2 ngFor如何计算循环值的数量?

使用ng-repeat或ngFor将JSON对象中的每个对象显示为单独的列表项

如何使用 angular2 ngFor 深度嵌套的 json 值

如何使用 Angular 访问 JSON 中的特定对象?