【问题标题】:Converting a multidimensional array to a specific object in Php 5.3 [duplicate]在 PHP 5.3 中将多维数组转换为特定对象 [重复]
【发布时间】:2017-06-20 00:37:19
【问题描述】:

我有一个像下面这样的 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 版本 - 5.3.29。

【问题讨论】:

  • json_decode() 是不是结果不够好,还是你没听说过这个简单的功能?
  • 我想将 json 转换为 Transfer_Sales_Order_ItemCollection 的实例。如何使用json_decode 实现这一目标?
  • 一个简单的不那不是我所追求的就足够了。我完全能够收到消息并删除我尝试的答案
  • 更新了为什么这不是重复的问题。

标签: php json


【解决方案1】:

你可以像这样在你的类上使用一个神奇的 __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;
}

【讨论】:

  • 它是否适用于 Php 5.3?
  • 操作。我的错。它没有。
  • 我编辑了答案,展示了如何用魔术方法完成它。
  • 在我的例子中,数组是多维的。例如[catalog_tax_class] => Transfer_Catalog_Tax_Class 所以我觉得一个简单的__set 不会。
  • 只需调整 __set 方法以检查 $prop 是否可以“转换为类名”,例如,如果可以,则实例化给定的类。
【解决方案2】:

您可以使用php's magic methods 来实现通用getter/setter 并将json 数据映射到您的内部结构。

这是一个如何使用它们的好例子:Getter and Setter?

【讨论】:

  • 你能举个例子吗?
  • 用一些例子添加了另一个讨论的链接。
猜你喜欢
  • 2017-02-16
  • 2014-03-08
  • 2020-08-10
  • 1970-01-01
  • 2013-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-28
相关资源
最近更新 更多