2013-08-01 52 views
2

我有問題的解碼JSON內容的所有URL映射......我試過了,但得到的錯誤:不是哈希參考...如何映射解碼JSON的Perl

$text = decode_json($document); 
#print Dumper($text); 
my @urls = map { $_->{'uri'} } @{$text->{children}->{children}}; 
print @urls; 

這裏解碼JSON,我需要捕捉到的網址......當我運行一個內部消除 - > {}兒我不明白的錯誤,但無法得到的網址只有一些vaules像parets,標題,類型等..

$VAR1 = { 
      'lastModified' => '1375048740407000', 
      'children' => [ 
          { 
          'parent' => 1, 
          'children' => [ 
              { 
               'parent' => 2, 
               'lastModified' => '1374933801475000', 
               'type' => 'text/x-moz-place-separator', 
               'id' => 15, 
               'title' => '', 
               'dateAdded' => '1374933801475000' 
              } 
              ], 
          'dateAdded' => '1374933800710000', 
          'lastModified' => '1375356010239000', 
          'title' => 'Bookmarks Menu', 
          'id' => 2, 
          'type' => 'text/x-moz-place-container', 
          'root' => 'bookmarksMenuFolder' 
          }, 
          { 
          'parent' => 1, 
          'children' => [ 
              { 
               'parent' => 3, 
               'charset' => 'UTF-8', 
               'uri' => 'http://projects.org/#', 
               'dateAdded' => '1375356057087000', 
               'lastModified' => '1375356057097000', 
               'title' => 'projects', 
               'id' => 24, 
               'type' => 'text/x-moz-place' 
              }, 
              { 
               'parent' => 3, 
               'charset' => 'UTF-8', 
               'uri' => 'http://kalja.org/', 
               'dateAdded' => '1375356063615000', 
               'index' => 1, 
               'lastModified' => '1375356063615000', 
               'title' => 'Kalja.org', 
               'id' => 25, 
               'type' => 'text/x-moz-place' 
              } 
              ], 
          'annos' => [ 
             { 
              'flags' => 0, 
              'value' => 'Add bookmarks to this folder to see them displayed on the Bookmarks Toolbar', 
              'name' => 'bookmarkProperties/description', 
              'type' => 3, 
              'mimeType' => undef, 
              'expires' => 4 
             } 
             ], 
          'dateAdded' => '1374933800710000', 
          'index' => 1, 
          'lastModified' => '1375356065865000', 
          'root' => 'toolbarFolder', 
          'title' => 'Bookmarks Toolbar', 
          'id' => 3, 
          'type' => 'text/x-moz-place-container' 
          }, 
          { 
          'parent' => 1, 
          'children' => [], 
          'dateAdded' => '1374933800710000', 
          'index' => 2, 
          'lastModified' => '1374933800710000', 
          'title' => 'Tags', 
          'id' => 4, 
          'type' => 'text/x-moz-place-container', 
          'root' => 'tagsFolder' 
          }, 
          { 
          'parent' => 1, 
          'children' => [], 
          'dateAdded' => '1374933800710000', 
          'index' => 3, 
          'lastModified' => '1375356065865000', 
          'title' => 'Unsorted Bookmarks', 
          'id' => 5, 
          'type' => 'text/x-moz-place-container', 
          'root' => 'unfiledBookmarksFolder' 
          } 
         ], 
      'root' => 'placesRoot', 
      'type' => 'text/x-moz-place-container', 
      'id' => 1, 
      'title' => '', 
      'dateAdded' => '1374933800710000' 
     }; 

謝謝提前

回答

3

你嵌套結構,所以你需要嵌套map's

my @urls = map { 
    map { $_->{'uri'} } @{$_->{children}} 
} 
@{$text->{children}}; 
+0

如果你想跳過沒有'uri'的孩子,那麼'map {$ _-> {'uri'}或()} ..' –