2015-11-05 69 views
0

我具有以下結構,它在變量my @json_device = getDeviceId($會話 - > {ID});當使用「嚴格參考」時,不能使用字符串(「[{」device_name「:」iPhone Simulato「...)作爲ARRAY參考

正在顯示的結構在下面的

$VAR1 = [{"device_name":"device1","device_id":"device_id1"},{"device_name":"device2","device_id":"device_id2"}] 

我使用以下代碼通過陣列裁判來迭代,並獲得「DEVICE_NAME」和「 DEVICE_ID」值

for my $aref (@json_device){ 
     for my $href (@$aref){ 
      warn $href->{device_name}; 
      warn $href->{device_uuid}; 
     } 
    } 

但我得到以下錯誤 不能使用字符串(「[{」device_name「:」iPhone Simulato「...)作爲ARRAY參考,而」嚴格參考「在使用 任何人都可以向我解釋什麼是去的錯誤?

回答

6

看起來getDeviceId不返回Perl數據,但JSON字符串。你必須轉換它:

use JSON; 

# .. 
my $json_device = getDeviceId($Session->{id}); 
my $aref  = decode_json($json_device); 
+0

並且還改變for循環如下?我的$ arefs($ aref){ 爲我的$ href(@ $ arefs){ warn $ href - > {device_name}; warn $ href - > {device_uuid}; } } – tsiro