-1

I have a json like below:-

{
  "_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
          }
        }
      }
    }
  ]
}

which I want to convert it to a specific object like below:-

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
                                        )

                                )

                        )

                )

        )
)

Can someone let me know how this can be achieved in a generic way as I am having quite a few json schemas like above?

I have gone through this question but none of the answer there seems to be quite apt for me.

I have gone through this question as well. The answers there does not suit me as in my case the attribute itself can be an object for example catalog_tax_class is of typeTransfer_Catalog_Tax_Class.

Php Version - 5.3.29.

Community
  • 1
  • 1
tuk
  • 5,941
  • 14
  • 79
  • 162
  • Is `json_decode()` not good enough a result or have you not heard about that simple function? – RiggsFolly Feb 02 '17 at 19:37
  • I want to convert the json to an instance of `Transfer_Sales_Order_ItemCollection`. How can I achieve this using `json_decode`? – tuk Feb 02 '17 at 19:40
  • A simple no thats not what I am after is quite good enough. I am quite able to get the message and delete my attempted answer – RiggsFolly Feb 02 '17 at 19:46
  • Updated the question why this is not duplicate. – tuk Feb 02 '17 at 20:20

2 Answers2

0

You can use a magic __set method on your class like this:

class Transfer_Sales_Order_ItemCollection
{
    // [...]
    public function __set($property, $value)
    {
        if (property_exists($this, $property)) {
            $this->$property = $value;
        }
    }
}

And do the following:

$obj = new Transfer_Sales_Order_ItemCollection();
foreach ($json as $prop => $value) {
    $obj->$prop = $value;
}
Vinicius Dias
  • 664
  • 3
  • 15
0

You can use php's magic methods to implement generic getters/setters and map the json data to your internal structure.

here is a good example how to use them: Getter and Setter?

Community
  • 1
  • 1
passioncoder
  • 909
  • 6
  • 10