2017-11-06 3 views
0

Hallo ich bin sehr neu auf dem angular2 ich die JSON iterieren wollen innerhalb JSON Meine JSON-Objekte ist unten dargestelltIterieren der JSON innerhalb JSON in Angular2

{"items":[{"id":564,"sku":"MB570Z\/B","name":"Mini DisplayPort to DVI Adapter","attribute_set_id":4,"price":2800,"status":1,"visibility":4,"type_id":"simple","created_at":"2017-08-31 04:49:57","updated_at":"2017-08-31 05:06:31","weight":1,"extension_attributes":[],"product_links":[],"tier_prices":[],"custom_attributes":[{"attribute_code":"description","value":"Every Mac with a Mini DisplayPort allows you to connect an external display or projector using an adapter. You can use an external display as your main workspace or to extend your desktop, or you can work in mirrored mode with a projector so you can view what your audience sees.\r\nUse the Mini DisplayPort to DVI Adapter to connect an advanced digital monitor, such as the 20- or 23-inch Apple Cinema Display, that includes a DVI connector. Review the documentation or check with the manufacturer of your monitor to make sure you're choosing the right adapter."},{"attribute_code":"short_description","value":"Mini DisplayPort to DVI Adapter"},{"attribute_code":"image","value":"\/m\/i\/mini_displayport_to_dvi_adapter.jpg"},{"attribute_code":"small_image","value":"\/a\/p\/apple_hdmi_to_dvi_adapter.jpg"},{"attribute_code":"thumbnail","value":"\/a\/p\/apple_hdmi_to_dvi_adapter.jpg"},{"attribute_code":"options_container","value":"container2"},{"attribute_code":"required_options","value":"0"},{"attribute_code":"has_options","value":"0"},{"attribute_code":"url_key","value":"mini-displayport-to-dvi-adapter"},{"attribute_code":"msrp_display_actual_price_type","value":"0"},{"attribute_code":"tax_class_id","value":"2"},{"attribute_code":"gift_message_available","value":"0"},{"attribute_code":"is_featured","value":"0"},{"attribute_code":"gst_source","value":"28"},{"attribute_code":"hsncode","value":"85444299"},{"attribute_code":"gst_source_after_minprice","value":"-1"}]},{"id":565,"sku":"MB571Z\/A","name":"Mini DisplayPort to Dual-Link DVI Adapter","attribute_set_id":4,"price":9400,"status":1,"visibility":4,"type_id":"simple","created_at":"2017-08-31 04:49:58","updated_at":"2017-08-31 05:06:31","weight":1,"extension_attributes":[],"product_links":[],"tier_prices":[],"custom_attributes":[{"attribute_code":"description","value":"Every Mac with a Mini DisplayPort allows you to connect an external display or projector using an adaptor. You can use an external display as your main workspace or to extend your desktop, or you can work in mirrored mode with a projector so you can view what your audience sees.\nUse the Mini DisplayPort to Dual-Link DVI Adaptor to connect your Mac to a 30-inch display that includes a DVI connector, such as the 30-inch Apple Cinema Display HD, and enjoy the ultimate widescreen canvas with a resolution of 2560 by 1600 pixels."},{"attribute_code":"short_description","value":"Mini DisplayPort to Dual-Link DVI Adapter"},{"attribute_code":"image","value":"\/m\/i\/mini_displayport_to_dual-link_dvi_adapter.jpg"},{"attribute_code":"small_image","value":"\/m\/i\/mini_displayport_to_dual-link_dvi_adapter.jpg"},{"attribute_code":"thumbnail","value":"\/m\/i\/mini_displayport_to_dual-link_dvi_adapter.jpg"},{"attribute_code":"options_container","value":"container2"},{"attribute_code":"required_options","value":"0"},{"attribute_code":"has_options","value":"0"},{"attribute_code":"url_key","value":"mini-displayport-to-dual-link-dvi-adapter"},{"attribute_code":"msrp_display_actual_price_type","value":"0"},{"attribute_code":"tax_class_id","value":"2"},{"attribute_code":"gift_message_available","value":"0"},{"attribute_code":"is_featured","value":"0"},{"attribute_code":"gst_source","value":"28"},{"attribute_code":"hsncode","value":"85444299"},{"attribute_code":"gst_source_after_minprice","value":"-1"}]}],"search_criteria":{"filter_groups":[],"page_size":2},"total_count":91} 
+0

Was genau meinen Sie mit "iterieren"? Meinst du das Anzeigen der Liste in der Benutzeroberfläche (ng-repeat)? –

+0

ja, aber Ng-Wiederholung funktioniert nicht * ngFor funktioniert für mich. Aber ich möchte nur Beschreibung in meiner Benutzeroberfläche anzeigen. –

+0

meinen Sie, dass Sie Daten in "custom_attributes" innerhalb von Elementen iterieren möchten? –

Antwort

0

Sie brauchen so etwas wie zu tun:

<div *ngFor="let item of items"> 
    // you can access your item here for example 
    <h1>item.id</h1> 
    // and you can loop again 
    <div *ngFor="let temp of item.custom_attributes"> 
    <p> temp.attribute_code</p> 
    </div> 
</div> 

können Sie * ngFor verwenden

ein Array von Objekten iterieren
+0

Ich möchte "Wert" des custom_attribute nur nicht attribute_code. Kannst du mir bitte etwas vorschlagen? –

+0

Sorry für falsche Informationen Ich wan Zuerst "Wert" im custom_attribute. –

+0

Sie können von custom_attributes auf was Sie wollen zugreifen. was meinen Sie mit dem ersten Wert? (custom_attributes [0]?) –

0

In Ihrem component.js,

private myItems:any = []; 

constructor(){ 
} 

yourFunction(){ 
    let vm = this; 
    items.forEach(function(val: any){ 
    val.custom_attributes.forEach(function(myAttrs: any){ 
     vm.myItems.push({ 
     "code": myAttrs.attribute_code, 
     "description": myAttrs.value 
     }); 
    }); 
    }); 
} 

in Ihrem HTML-Datei

<div *ngFor="let myItem of myItems"> 
    <p> Code: {{myItem.code}} </p> 
    <p> Description : {{myItem.description}} 
</div>