1

当我尝试在 python 程序中使用它时,我在 PHP 中的 web 服务的序列化响应方面遇到了一些问题。

我使用 PHPSerializer 库,但它似乎没有正确解码字符串。这是 php webservice 响应的内容:

b'a:43:{i:0;s:26:"BlackBerry_Bold_9000_Phone";i:1;s:27:"BlackBerry_Curve_8310_Phone";i:2;s:30:"Kyocera_Wildcard_Prepaid_Phone";i:3;s:19:"T_Mobile_Wing_Phone";i:4;s:44:"Samsung_LN22A450_22_Inch_720p_LCD_HDTV_Black";i:5;s:39:"Samsung_LN40A550_40_Inch_1080p_LCD_HDTV";i:6;s:64:"Samsung_LN46A630_46_Inch_1080p_120Hz_LCD_HDTV_with_Red_Touch_of_";i:7;s:61:"Sony_Bravia_W_Series_KDL_52W4100_52_Inch_1080p_120Hz_LCD_HDTV";i:8;s:60:"Denon_AVR_3808_Home_Theater_Bundle_with_MartinLogan_Speakers";i:9;s:55:"Denon_AVR_888_Home_Theater_Bundle_with_Klipsch_Speakers";i:10;s:49:"Energy_5.1_Take_Classic_Home_Entertainment_System";i:11;s:46:"LG_LFD850_Home_theater_system_with_iPod_cradle";i:12;s:52:"Onkyo_HT_S4100_5.1_Channel_Home_Entertainment_System";i:13;s:39:"Canon_EOS_40D_10.1MP_Digital-SLR_Camera";i:14;s:40:"Canon_Rebel_XS_10.1MP_Digital_SLR_Camera";i:15;s:58:"Canon_Rebel XS_10.1MP_Digital_SLR_Camera_with_EF_S_18_55mm";i:16;s:42:"Olympus_Evolt_E520_10MP_Digital_SLR_Camera";i:17;s:44:"Air_King_9166_20_inch_Whole_House_Window_Fan";i:18;s:51:"Bionaire_BW2300_Twin_Window_Fan_with_Remote_Control";i:19;s:32:"Holmes_HAWF_2041_Twin_Window_Fan";i:20;s:32:"Lasko_Reversible_Twin_Window_Fan";i:21;s:56:"Black_Decker_F930_Light_N_Easy_Smart_Steam_Nonstick_Iron";i:22;s:48:"Jerdon_First_Class_J513W_Iron_with_Dual_Auto_Off";i:23;s:64:"Jerdon_First_Class_J913W_Iron_with_Dual_Auto_Off_and_Retractable";i:24;s:27:"T_FAL_Aquaspeed_FV5155_Iron";i:25;s:45:"Cuisinart_DLC_2_Mini_Prep_Plus_Food_Processor";i:26;s:29:"Cuisinart_Mini_Prep_Processor";i:27;s:42:"KitchenAid_700_Watt_12_Cup_Food_Processors";i:28;s:46:"KitchenAid_7_Cup_Food_Processor_with_Mini_Bowl";i:29;s:46:"Dirt_Devil_082700_Vision_Turbo_Canister_Vacuum";i:30;s:42:"Electrolux_EL_6988D_Oxygen_Canister_Vacuum";i:31;s:54:"Electrolux_EL6985B_Harmony_Ultra_Quiet_Canister_Vacuum";i:32;s:62:"Hoover_S3765-040 WindTunnel_Electronic_Bagless_Canister_Vacuum";i:33;s:23:"Canon_PowerShot_A590_IS";i:34;s:28:"microwaves_Whirlpool_AMW_460";i:35;s:21:"Motorola_MOTORIZR_Z10";i:36;s:18:"Electrolux_ZB_412C";i:37;s:27:"Yamaha_PianoCraft_MCR_E_320";i:38;s:21:"Panasonic_NV_GS85EE_S";i:39;s:5:"31669";i:40;s:5:"31780";i:41;s:5:"16101";i:42;s:0:"";}'

这就是 python 程序在“反序列化”之后的内容:

{0: b'BlackBerry_Bold_9000_Phone', 1: b'BlackBerry_Curve_8310_Phone', 2: b'Kyocera_Wildcard_Prepaid_Phone', 3: b'T_Mobile_Wing_Phone', 4: b'Samsung_LN22A450_22_Inch_720p_LCD_HDTV_Black', 5: b'Samsung_LN40A550_40_Inch_1080p_LCD_HDTV', 6: b'Samsung_LN46A630_46_Inch_1080p_120Hz_LCD_HDTV_with_Red_Touch_of_', 7: b'Sony_Bravia_W_Series_KDL_52W4100_52_Inch_1080p_120Hz_LCD_HDTV', 8: b'Denon_AVR_3808_Home_Theater_Bundle_with_MartinLogan_Speakers', 9: b'Denon_AVR_888_Home_Theater_Bundle_with_Klipsch_Speakers', 10: b'Energy_5.1_Take_Classic_Home_Entertainment_System', 11: b'LG_LFD850_Home_theater_system_with_iPod_cradle', 12: b'Onkyo_HT_S4100_5.1_Channel_Home_Entertainment_System', 13: b'Canon_EOS_40D_10.1MP_Digital-SLR_Camera', 14: b'Canon_Rebel_XS_10.1MP_Digital_SLR_Camera', 15: b'Canon_Rebel XS_10.1MP_Digital_SLR_Camera_with_EF_S_18_55mm', 16: b'Olympus_Evolt_E520_10MP_Digital_SLR_Camera', 17: b'Air_King_9166_20_inch_Whole_House_Window_Fan', 18: b'Bionaire_BW2300_Twin_Window_Fan_with_Remote_Control', 19: b'Holmes_HAWF_2041_Twin_Window_Fan', 20: b'Lasko_Reversible_Twin_Window_Fan', 21: b'Black_Decker_F930_Light_N_Easy_Smart_Steam_Nonstick_Iron', 22: b'Jerdon_First_Class_J513W_Iron_with_Dual_Auto_Off', 23: b'Jerdon_First_Class_J913W_Iron_with_Dual_Auto_Off_and_Retractable', 24: b'T_FAL_Aquaspeed_FV5155_Iron', 25: b'Cuisinart_DLC_2_Mini_Prep_Plus_Food_Processor', 26: b'Cuisinart_Mini_Prep_Processor', 27: b'KitchenAid_700_Watt_12_Cup_Food_Processors', 28: b'KitchenAid_7_Cup_Food_Processor_with_Mini_Bowl', 29: b'Dirt_Devil_082700_Vision_Turbo_Canister_Vacuum', 30: b'Electrolux_EL_6988D_Oxygen_Canister_Vacuum', 31: b'Electrolux_EL6985B_Harmony_Ultra_Quiet_Canister_Vacuum', 32: b'Hoover_S3765-040 WindTunnel_Electronic_Bagless_Canister_Vacuum', 33: b'Canon_PowerShot_A590_IS', 34: b'microwaves_Whirlpool_AMW_460', 35: b'Motorola_MOTORIZR_Z10', 36: b'Electrolux_ZB_412C', 37: b'Yamaha_PianoCraft_MCR_E_320', 38: b'Panasonic_NV_GS85EE_S', 39: b'31669', 40: b'31780', 41: b'16101', 42: b''}

现在我想做的是将所有产品名称放在一个树视图中,根据这个代码片段:

from phpserialize import serialize, unserialize
entry1 = gu.get_object("entry1")
#
str1 = entry1.get_text() # "/onticreadservice.php?op=listar_skus"
fh = urllib.request.urlopen(str1)
html = fh.read()
fh.close()
# http://dev.ontic.es:8443/magento/onticreadservice.php?op=listar_skus
print(html)
obj = unserialize(html)
print(obj)
store = Gtk.ListStore(str)
for c in obj:
   store.append(c)
leftTree = gu.get_object("treeview1")

renderer = Gtk.CellRendererText()
column1 = Gtk.TreeViewColumn("Nombre", renderer, text=0)
leftTree.append_column(column1)
leftTree.set_model(store)

但是程序说'c'是一个int。我怎么能只检索产品名称?

提前致谢

4

1 回答 1

1

通过循环遍历字典obj;这些都是整数。

如果您想存储,请遍历obj.itervalues()序列(或obj.values()在使用 Python 3 时使用):

for value in obj.itervalues():
   store.append(value)

看起来好像原始的 PHP 对象是一个列表(键都是顺序的);您可能希望按排序顺序访问值:

for key in sorted(obj):
    store.append(obj[key])
于 2013-08-09T12:08:51.397 回答