drz5553 2013-08-14 07:08
浏览 73
已采纳

Object_hook没有给出phpserialize

I'm developing a python app using the library phpserialize. The problem is that now I'm facing an error and I can't find a way to solve it: I get this serialized data from a php web service:

a:5:{s:10:"attributes";a:53:{s:9:"entity_id";s:2:"97";s:14:"entity_type_id";s:1:"4";s:16:"attribute_set_id";s:1:"4";s:7:"type_id";s:6:"simple";s:3:"sku";s:5:"16101";s:10:"created_at";s:19:"2013-05-29 14:59:05";s:10:"updated_at";s:19:"2013-08-08 07:38:41";s:11:"has_options";s:1:"1";s:16:"required_options";s:1:"1";s:4:"name";s:65:"Cuchillo Tactico Combat-Tec negro Aitor dureza de 55/58 HRc 16101";s:10:"meta_title";s:87:"Comprar Cuchillo Tactico Combat-Tec negro Aitor dureza de 55/58 HRc 16101 8435076573875";s:16:"meta_description";s:200:"Comprar Cuchillo Tactico Combat-Tec negro Aitor dureza de 55/58 HRc 16101 8435076573875 hasta un 50% más económico que en tiendas. Miles de referencias a tu disposición. Envío Urgente. Infórmate!";s:5:"image";s:17:"/1/6/16101_37.png";s:11:"small_image";s:17:"/1/6/16101_37.png";s:9:"thumbnail";s:17:"/1/6/16101_37.png";s:7:"url_key";s:39:"cuchillo-tactico-combat-tec-negro-aitor";s:8:"url_path";s:44:"cuchillo-tactico-combat-tec-negro-aitor.html";s:13:"custom_design";N;s:17:"options_container";s:10:"container2";s:22:"gift_message_available";N;s:11:"page_layout";N;s:22:"country_of_manufacture";N;s:12:"msrp_enabled";s:1:"2";s:30:"msrp_display_actual_price_type";s:1:"4";s:7:"videoid";N;s:13:"c2c_long_hoja";s:4:"16.5";s:16:"c2c_espesor_hoja";s:3:"4.5";s:12:"manufacturer";N;s:5:"color";s:1:"1";s:6:"status";s:1:"1";s:12:"tax_class_id";s:1:"5";s:10:"visibility";s:1:"4";s:21:"enable_googlecheckout";s:1:"1";s:12:"is_recurring";s:1:"0";s:11:"is_imported";s:1:"0";s:9:"proveedor";s:2:"43";s:5:"price";s:7:"57.8100";s:6:"weight";s:6:"0.0000";s:13:"special_price";N;s:4:"cost";N;s:4:"msrp";N;s:17:"special_from_date";N;s:15:"special_to_date";N;s:14:"news_from_date";N;s:12:"news_to_date";N;s:18:"custom_design_from";N;s:16:"custom_design_to";N;s:11:"description";s:1672:"<p>Cuchillo AITOR modelo Combat-Tec con hoja negra de 16,5 Cm. con sierra superior. Acero Molibdeno Vanadio con prueba de dureza. Mango de micarta negra. Incluye funda de poliamida negra.</p><br /><table id="product-attribute-specs-table" class="data-table"><colgroup><col width="25%" /> <col /> </colgroup><br /><tbody><br /><trclass="first odd"><th class="label">C&oacute;digo</th><br /><td class="data last">16101</td><br /></tr><br /><tr class="even"><th class="label">Marcas</th><br /><td class="data last">Aitor</td><br /></tr><br /><tr class="odd"><thclass="label">Longitud hoja</th><br /><td class="data last">16.5</td><br /></tr><br /><tr class="even"><th class="label">Longitud total</th><br /><td class="data last">28</td><br /></tr><br /><tr class="odd"><th class="label">Material hoja</th><br /><td class="data last">Acero Aitor al cromo negro</td><br /></tr><br /><trclass="even"><th class="label">Funda</th><br /><td class="data last">No</td><br /></tr><br /><tr class="odd"><th class="label">Espesor hoja</th><br /><td class="data last">4.5</td><br /></tr><br /><tr class="even"><th class="label">Material mango</th><br /><td class="data last">Micarta</td><br /></tr><br /><tr class="odd"><th class="label">Presentaci&oacute;n</th><br /><td class="data last">Estuche de cart&oacute;n</td><br /></tr><br /><tr class="even"><thclass="label">Observaciones</th><br /><td class="data last">No</td><br /></tr><br /><tr class="odd"><th class="label">Peso</th><br /><td class="data last">291</td><br /></tr><br /><tr class="last even"><th class="label">C&oacute;digo EAN</th><br /><td class="data last">8435076573875</td><br /></tr><br /></tbody><br /></table>";s:17:"short_description";s:88:"Cuchillo Tactico Combat-Tec negro de la marca Aitor dureza de 55/58 HRc Referencia 16101";s:12:"meta_keyword";s:87:"Comprar Cuchillo Tactico Combat-Tec negro Aitor dureza de 55/58 HRc 16101 8435076573875";s:20:"custom_layout_update";N;s:10:"is_salable";s:1:"1";s:10:"stock_item";O:13:"Varien_Object":7:{s:8:"*_data";a:1:{s:11:"is_in_stock";s:1:"1";}s:18:"*_hasDataChanges";b:0;s:12:"*_origData";N;s:15:"*_idFieldName";N;s:13:"*_isDeleted";b:0;s:16:"*_oldFieldsMap";a:0:{}s:17:"*_syncFieldsMap";a:0:{}}}s:6:"custom";a:2:{s:9:"drop_down";a:1:{s:5:"abcde";a:3:{i:0;s:3:"123";i:1;s:3:"321";i:2;s:3:"231";}}s:5:"radio";a:1:{s:10:"Jajejijoju";a:3:{i:0;s:1:"G";i:1;s:1:"M";i:2;s:1:"P";}}}s:10:"categorias";a:5:{i:0;a:1:{s:16:"Default Category";s:12:"Root Catalog";}i:1;a:1:{s:7:"Default";s:16:"Default Category";}i:2;a:1:{s:12:"Cuchillería";s:7:"Default";}i:3;a:1:{s:9:"Cuchillos";s:12:"Cuchillería";}i:4;a:1:{s:31:"Cuchillos Militares y tácticos";s:9:"Cuchillos";}}s:10:"inventario";s:8:"999.0000";s:7:"galeria";a:1:{i:0;a:3:{s:3:"url";s:71:"http://foo.es/bar.png";s:5:"label";s:0:"";s:8:"position";s:1:"1";}}}

And when unserializing it, the following error is displayed:

raise ValueError('object in serialization dump but ' ValueError: object in serialization dump but object_hook not given.

I've googled the error, but I only end up in the source code. Do you have any idea?

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • douna1941 2013-08-14 08:06
    关注

    The documentation on the PyPI page of phpserialize contains some documentation, namely:

    An object hook is a function that takes the name of the class and a dict with the instance data as arguments. The instance data keys are in PHP format which usually is not what you want. To convert it into Python identifiers you can use the convert_member_dict function. For more information about that, have a look at the next section. Here an example for a simple object hook:

    >>> class User(object):
    ...     def __init__(self, username):
    ...         self.username = username
    ...
    >>> def object_hook(name, d):
    ...     cls = {'WP_User': User}[name]
    ...     # The following was added by me -Antti
    ...     d = convert_member_dict(d)
    ...     return cls(**d)
    ...
    >>> user = loads(data, object_hook=object_hook)
    >>> user.username
    'admin'
    

    Thus for all classes not supported by phpserialize directly, you need to write a matching class in Python and return instances of them from the object_hook.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度