json - 如何映射解码的 json perl

标签 json perl perl-hash

我在映射解码的 json 内容中的所有 url 时遇到问题...我尝试过,但收到错误:不是哈希引用...

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

这里解码了 JSON,我需要捕获 url...当我在没有 ->{children} 的情况下运行时,我没有收到错误,但无法获取 url,只有一些值,如 parets、title、type 等。 .

$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'
        };

提前致谢

最佳答案

您有嵌套结构,因此您需要嵌套 map

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

关于json - 如何映射解码的 json perl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17993368/

相关文章:

windows - 在没有安装 ActivePerl 的情况下在 Windows 上使用 perl 脚本?

c++ - 哈希表的性能,为什么C++最慢?

linux - 如何在 perl 中实现 linux 查询?

arrays - perl - 遍历数组时允许对数组进行操作

perl - 这两个定义有什么区别?

javascript - 从 Google Apps Script 中的 api 解析 JSON 响应

javascript - 无法在 json 数组对象迭代中获得所需或类似的输出?

php - 单击按钮时将 JSON 从 PHP 返回到 ajax

来自哈希引用的 Perl 访问值

c# - 如何实现将类型附加到属性名称的 json 自定义序列化?