Thread Hash in JSON finden (21 answers)
Opened by cbxk1xg at 2013-11-14 15:42

Linuxer
 2013-11-14 16:26
#172094 #172094
User since
2006-01-27
3869 Artikel
HausmeisterIn

user image
Was mir noch einfiel:

Alternativ zu keys() kannst Du auch values() verwenden.

Mein Beispiel würde dadurch etwas kürzer:

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use strict;
use warnings;
use JSON;
use Data::Dumper;

my $text = do { local $/; <DATA> };
my $json = JSON->new();

my $data = $json->decode($text);

#print Dumper( $data );

for my $page ( values %{ $data->{query}->{pages} } ) {
  print $page->{extract}, "\n";
}

__DATA__
{
    "query": {
        "pages": {
            "92491": {
                "pageid": 92491,
                "ns": 0,
                "title": "Eric Clapton",
                "extract": "foo ... bar"
            }
        }
    }
}


Wenn Du sicher bist/stellen kannst, dass nur eine Seite als Ergebnis vorliegt, dann kannst Du es auch so machen:

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
use strict;
use warnings;
use JSON;
use Data::Dumper;

my $text = do { local $/; <DATA> };
my $json = JSON->new();

my $data = $json->decode($text);

#print Dumper( $data );

my ($page) = values %{ $data->{query}->{pages} };
print $page->{extract}, "\n";


__DATA__
{
    "query": {
        "pages": {
            "92491": {
                "pageid": 92491,
                "ns": 0,
                "title": "Eric Clapton",
                "extract": "foo ... bar"
            }
        }
    }
}
meine Beiträge: I.d.R. alle Angaben ohne Gewähr und auf Linux abgestimmt!
Die Sprache heisst Perl, nicht PERL. - Bitte Crossposts als solche kenntlich machen!

View full thread Hash in JSON finden