我目前正在进行一个项目,使用 API 检索、更新和删除数据。我使用的 API 是 prestashop API。所以在能够检索数据和更新一些项目后,我遇到了一个问题。根据文档中所说,通过 API 发送和检索的所有数据都是使用 json
和 xml
。由于 API 的某些数据在 json 返回中具有不同级别,如 @attributes 和 @associations 级别,因此我想问这个问题。
问题是,我想访问这些数据,并与 angularjs 结合使用来显示这些数据。让我给您展示一个我试图实现的快速示例。
首先,JSON
返回将是这样的。
{"products":{"product":[{"id":"1","id_manufacturer":"1","id_supplier":"1","id_category_default":"5","new":{},"cache_default_attribute":"1","id_default_image":"1","id_default_combination":"1","id_tax_rules_group":"1","position_in_category":"0","manufacturer_name":"Fashion Manufacturer","quantity":"0","type":"simple","id_shop_default":"1","reference":"demo_1","supplier_reference":{},"location":{},"width":"0.000000","height":"0.000000","depth":"0.000000","weight":"0.000000","quantity_discount":"0","ean13":"333456789111","isbn":{},"upc":{},"cache_is_pack":"0","cache_has_attachments":"0","is_virtual":"0","state":"1","on_sale":"0","online_only":"0","ecotax":"0.000000","minimal_quantity":"1","price":"16.510000","wholesale_price":"4.950000","unity":{},"unit_price_ratio":"0.000000","additional_shipping_cost":"0.00","customizable":"0","text_fields":"0","uploadable_files":"0","active":"1","redirect_type":"404","id_type_redirected":"0","available_for_order":"1","available_date":"0000-00-00","show_condition":"0","condition":"new","show_price":"1","indexed":"1","visibility":"both","advanced_stock_management":"0","date_add":"2017-03-16 14:36:24","date_upd":"2017-12-01 13:01:13","pack_stock_type":"3","meta_description":{"language":{"@attributes":{"id":"1"}}},"meta_keywords":{"language":{"@attributes":{"id":"1"}}},"meta_title":{"language":{"@attributes":{"id":"1"}}},"link_rewrite":{"language":"gebleekte-T-shirts-met-korte-mouwen"},"name":{"language":"Gebleekte T-shirts met Korte Mouwen"},"description":{"language":"
Fashion maakt goed ontworpen collecties sinds 2010. Het merk biedt vrouwelijke combineerbare kleding en statement dresses en heeft een pr\u00eat-\u00e0-porter collectie ontwikkeld met kledingstukken die niet in een garderobe mogen ontbreken. Het resultaat? Cool, gemakkelijk, easy, chique met jeugdige elegantie en een duidelijk herkenbare stijl. Alle prachtige kledingstukken worden met de grootste zorg gemaakt in Itali\u00eb. Fashion breidt zijn aanbod uit met accessoires zoals schoenen, hoeden, riemen!<\/p>"},"description_short":{"language":"
Gebleekt T-shirt met korte mouwen en hoge halslijn. Zacht en elastisch materiaal zorgt voor een comfortabele pasvorm. Maak het af met een strooien hoed en u bent klaar voor de zomer!<\/p>"},"available_now":{"language":"Op voorraad"},"available_later":{"language":{"@attributes":{"id":"1"}}},"associations":{"categories":{"@attributes":{"nodeType":"category","api":"categories"},"category":[{"id":"2"},{"id":"3"},{"id":"4"},{"id":"5"}]},"images":{"@attributes":{"nodeType":"image","api":"images"},"image":[{"id":"1"},{"id":"2"},{"id":"3"},{"id":"4"}]},"combinations":{"@attributes":{"nodeType":"combination","api":"combinations"},"combination":[{"id":"1"},{"id":"2"},{"id":"3"},{"id":"4"},{"id":"5"},{"id":"6"}]},"product_option_values":{"@attributes":{"nodeType":"product_option_value","api":"product_option_values"},"product_option_value":[{"id":"1"},{"id":"13"},{"id":"14"},{"id":"2"},{"id":"3"}]},"product_features":{"@attributes":{"nodeType":"product_feature","api":"product_features"},"product_feature":[{"id":"5","id_feature_value":"5"},{"id":"6","id_feature_value":"11"},{"id":"7","id_feature_value":"17"}]},"tags":{"@attributes":{"nodeType":"tag","api":"tags"}},"stock_availables":{"@attributes":{"nodeType":"stock_available","api":"stock_availables"},"stock_available":[{"id":"1","id_product_attribute":"0"},{"id":"11","id_product_attribute":"1"},{"id":"12","id_product_attribute":"2"},{"id":"13","id_product_attribute":"3"},{"id":"22","id_product_attribute":"4"},{"id":"23","id_product_attribute":"5"},{"id":"24","id_product_attribute":"6"}]},"accessories":{"@attributes":{"nodeType":"product","api":"products"}},"product_bundle":{"@attributes":{"nodeType":"product","api":"products"}}}},
简化结构products {
product {
id:
name:
category:
...
@attributes {
id:
language:
...
}
@attributes {
{"nodeType":"product_option_value","api":"product_option_values"},"product_option_value":[
{"id":"1"},
{"id":"11"},
{"id":"8"},
{"id":"2"},
{"id":"3"}
]
},
}
}
}
在AngularJS中,使用$http.get()
函数,我能够检索数据并使用ng-repeat和绑定组合来显示产品名称。现在我想访问@attribute值等信息。但是我该如何访问它们?是否有特定的方法可以做到这一点?还是纯粹通过访问JSON对象的深度级别来完成?
产品的AngularJS函数:
$http.get('config/get/getProducts.php', {cache: true}).then(function (response) {
$scope.products = response.data.products.product
});
那么在 <html>
中我只需使用:
<div ng-if="product.active == 1" class="productimg col-4" ng-repeat="product in products | filter : {id_category_default: catFilter.id}: true | filter:productSearch | filter:product.name | orderBy: 'name'">
<p ng-bind="product.name.language"></p>
</div>
更新:01/02/2018 读完并测试了一些评论后,我得出了一个合理的解决方案。我能够访问@attributes和associations值,但我遇到了一个新问题。每个过滤器返回的结果都是多个"id"值。看下面的示例。
<div class="col-lg-3" ng-repeat="value in products">
<p ng-bind="value.associations.categories.category"></p>
</div>
返回:
[{"id":"2"},{"id":"3"},{"id":"4"},{"id":"5"}]
[{"id":"2"},{"id":"3"},{"id":"4"},{"id":"5"}]
[{"id":"2"},{"id":"3"},{"id":"4"},{"id":"7"}]
每行 [.. ..] 代表不同的产品。现在我需要获取这些值,仅作为它们所表示的数字,以便与不同表格中对应的id值进行比较。一个好的结果应该是:
2, 3, 4, 5
我该如何得到这个解决方案?
如果有人对此感兴趣,我正在尝试通过prestashop webservice从prestashop安装中检索产品的option_values
id和category
id。