我有一个像下面这样的json: -
{
"_objectClassName": "Transfer_Sales_Order_Item",
"_idAttribute": "id_sales_order_item",
"_indexDirty": true,
"_data": [
{
"_attributes": {
"history_collection": "Transfer_Sales_Order_Item_Status_HistoryCollection",
"merchant": "Transfer_Sales_Order_Item_Merchant",
"shipment": "Transfer_Sales_Order_Item_Shipment",
"status": "Transfer_Sales_Order_Item_Status",
"fk_catalog_shipment_type": null,
"warehouse": "Transfer_Sales_Order_Address",
"brand": null,
"root_category": null,
"category": null,
"history_log_note": null,
"delivery_time": null,
"bundle_discount": null,
"fk_sku_bundle": null,
"is_freebie": null,
"shipping_charge": null,
"not_buyable": null,
"bundle_group": null,
"dispatch_time": null,
"info_collection": "Transfer_Sales_Order_Item_Additional_Info",
"trackinginfo_collection": "Transfer_Sales_Order_Shipment_Tracking_HistoryCollection",
"trakinginfo_summarized": null,
"status_history": null,
"text_collection": "Transfer_Sales_Order_Item_Custom_TextCollection",
"returninfo_summarized": null,
"refundinfo_summarized": null,
"shipping_special_service_info": "Transfer_Sales_Order_Item_Shipping_Special_Service",
"payback_credits_value": null,
"stock_version": null,
"fk_catalog_simple": null,
"item_attributes": "Transfer_Sales_Order_Item_Attributes"
},
"_defaultValues": {
"quantity": 1
},
"_data": {
"sku": "CA028SH10UTLINDFAS-697382",
"fk_catalog_simple": 697382,
"name": "Bronze Sandal",
"unit_price": 1095,
"catalog_tax_class": {
"_attributes": {
"tax_percent": null
},
"_defaultValues": [
],
"_data": {
"tax_percent": 0
}
},
"tax_percent": 0,
"original_unit_price": 1095,
"shipping_charge": 1,
"item_attributes": {
"_attributes": {
"is_surface": null,
"is_fragile": null,
"is_precious": null,
"seller_discount": 0,
"seller_price": 0,
"seller_sku": null
},
"_defaultValues": [
],
"_data": {
"is_surface": 0,
"is_fragile": 0,
"seller_sku": 1000211001,
"seller_price": 0
}
}
}
}
]
}
我想将它转换为如下特定对象: -
Transfer_Sales_Order_ItemCollection Object
(
[_objectClassName:protected] => Transfer_Sales_Order_Item
[_idAttribute:protected] => id_sales_order_item
[_indexDirty:Transfer_AbstractCollection:private] => 1
[_data:protected] => Array
(
[0] => Transfer_Sales_Order_Item Object
(
[_attributes:protected] => Array
(
[history_collection] => Transfer_Sales_Order_Item_Status_HistoryCollection
[merchant] => Transfer_Sales_Order_Item_Merchant
[shipment] => Transfer_Sales_Order_Item_Shipment
[status] => Transfer_Sales_Order_Item_Status
[fk_catalog_shipment_type] =>
[warehouse] => Transfer_Sales_Order_Address
[brand] =>
[root_category] =>
[category] =>
[history_log_note] =>
[delivery_time] =>
[bundle_discount] =>
[fk_sku_bundle] =>
[is_freebie] =>
[shipping_charge] =>
[not_buyable] =>
[bundle_group] =>
[dispatch_time] =>
[info_collection] => Transfer_Sales_Order_Item_Additional_Info
[trackinginfo_collection] => Transfer_Sales_Order_Shipment_Tracking_HistoryCollection
[trakinginfo_summarized] =>
[status_history] =>
[text_collection] => Transfer_Sales_Order_Item_Custom_TextCollection
[returninfo_summarized] =>
[refundinfo_summarized] =>
[shipping_special_service_info] => Transfer_Sales_Order_Item_Shipping_Special_Service
[payback_credits_value] =>
[stock_version] =>
[fk_catalog_simple] =>
[item_attributes] => Transfer_Sales_Order_Item_Attributes
)
[_defaultValues:protected] => Array
(
[quantity] => 1
)
[_data:protected] => Array
(
[sku] => CA028SH10UTLINDFAS-697382
[fk_catalog_simple] => 697382
[name] => Bronze Sandal
[unit_price] => 1095
[catalog_tax_class] => Transfer_Catalog_Tax_Class Object
(
[_attributes:protected] => Array
(
[tax_percent] =>
)
[_defaultValues:protected] => Array
(
)
[_data:protected] => Array
(
[tax_percent] => 0
)
)
[tax_percent] => 0
[original_unit_price] => 1095
[shipping_charge] => 1
[item_attributes] => Transfer_Sales_Order_Item_Attributes Object
(
[_attributes:protected] => Array
(
[is_surface] =>
[is_fragile] =>
[is_precious] =>
[seller_discount] => 0
[seller_price] => 0
[seller_sku] =>
)
[_defaultValues:protected] => Array
(
)
[_data:protected] => Array
(
[is_surface] => 0
[is_fragile] => 0
[seller_sku] => 1000211001
[seller_price] => 0
)
)
)
)
)
)
有人能让我知道如何以通用的方式实现这一点,因为我有很多像上面这样的json模式吗?
我已经完成了question,但似乎没有一个答案适合我。
我也经历了这个question。那里的答案不适合我,因为在我的情况下,属性本身可以是一个对象,例如catalog_tax_class
的类型为Transfer_Catalog_Tax_Class
。
Php Version - 5.3.29。
答案 0 :(得分:0)
您可以在类上使用magic __set方法,如下所示:
class Transfer_Sales_Order_ItemCollection
{
// [...]
public function __set($property, $value)
{
if (property_exists($this, $property)) {
$this->$property = $value;
}
}
}
并执行以下操作:
$obj = new Transfer_Sales_Order_ItemCollection();
foreach ($json as $prop => $value) {
$obj->$prop = $value;
}
答案 1 :(得分:0)
您可以使用php's magic methods实现通用getter / setter并将json数据映射到内部结构。
这是一个很好的例子:Getter and Setter?