2017-02-02 61 views
-1

我有一個JSON象下面這樣: -在Php轉換多維數組中的特定對象5.3

{ 
    "_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_classTransfer_Catalog_Tax_Class類型。

Php Version - 5.3.29。

+0

是'json_decode()'不夠好結果,或者你有沒有聽說過簡單的功能? – RiggsFolly

+0

我想將json轉換爲'Transfer_Sales_Order_ItemCollection'實例。我怎樣才能實現這個使用'json_decode'? – tuk

+0

一個簡單的沒有,這不是我所追求的是相當好。我很能夠得到消息並刪除我的嘗試答案 – RiggsFolly

回答

0

您可以在您的課上使用魔法__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; 
} 
+0

它是否適用於Php 5.3? – tuk

+0

行動。我的錯。它不是。 –

+0

我編輯了答案,展示瞭如何用神奇的方法來實現它。 –

0

您可以使用php's magic methods來實現通用的getter/setter和映射JSON數據到你的內部結構。

這裏是一個很好的例子,如何使用它們:Getter and Setter?

+0

你能舉一個例子,說明這些可以用在我的案例中嗎? – tuk

+0

用一些例子添加了一個到另一個討論的鏈接。 – passioncoder