Decoding and using JSON data in Perl

I am confused about accessing the contents of some JSON data that I decrypted. Here is an example

I do not understand why this solution works, but my own does not. My questions are rephrased below

my $json_raw = getJSON(); 
my $content  = decode_json($json_raw);
print Data::Dumper($content);

At this point, my JSON data was converted to this

$VAR1 = { 'items' => [ 1, 2, 3, 4 ] };

My guess tells me that after decoding, the object will be a hash with one element that has a key itemsand an array reference as a value.

$content{'items'}[0]

where it $content{'items'}gets a reference to the array, and the external one $...[0]gets access to the first element of the array and interprets it as a scalar. However, this will not work. I get an erroruse of uninitialized value [...]

However, the following works:

$content->{items}[0]

where $content->{items}gives a reference to the array and [0]refers to the first element of this array.

Questions

  • $content{'items'} ? @{content{'items'}}, , , content{'items'}, . .

  • ?

+4
4

:) , , , , , .

use strict;    #use this all times
use warnings;  #this too - helps a lot!
use JSON;

my $json_str = '    { "items" : [ 1, 2, 3, 4 ] }    ';
my $content = decode_json($json_str);

:

, , .

, , decode_json , , . ( )

UTF-8 () JSON UTF-8, .

my $content = decode_json($json_str);

SCALAR ( ).

: , :

printf "reftype:%s\n", ref($content);
#print: reftype:HASH       ^   
           #therefore the  +------- is a SCALAR value containing a reference to hash

hashref -

print "key: $_\n" for keys %{$content}; #or in short %$content
#prints: key: items

"items" (arrayref)

my $aref = $content->{items};   #$hashref->{key}
#or
#my $aref = ${$content}{items}; #$hash{key}

#my $aref = $content{items};    #throws error if "use strict;"
#Global symbol "%content" requires explicit package name at script.pl line 20.

$content{item} %content, / ​​. $content - , - %content.

{
    #in perl 5.20 you can also
    use 5.020;
    use experimental 'postderef';
    print "key-postderef: $_\n" for keys $content->%*;
}

- arrayref -

printf "reftype:%s\n", ref($aref);
#reftype:ARRAY

print "arr-item: $_\n" for @{$aref};

#print "$_\n" for @aref;
#dies: Global symbol "@aref" requires explicit package name at script.pl line 37.

{
    #in perl 5.20 you can also
    use 5.020;
    use experimental 'postderef';
    print "aref-postderef: $_\n" for $aref->@*;
}

:

my @arr;               #array variable
my $arr_ref = \@arr;   #scalar - containing a reference to @arr

@{$arr_ref} is the same as @arr 
 ^^^^^^^^^^ - array reference in curly brackets

$arrayref - @{$array_ref} , .

my %hash;              #hash variable
my $hash_ref = \%hash; #scalar - containing a reference to %hash

%{$hash_ref} is the same as %hash
 ^^^^^^^^^^^ - hash reference in curly brackets

$hash_ref - %{$hash_ref} , .

say $content->{items}->[0];
say $content->{items}[0];
say ${$content}{items}->[0];
say ${$content}{items}[0];
say ${$content->{items}}[0];
say ${${$content}{items}}[0];

1.

+5

$content - -, . $content{items} %content, . , " ".

+4

:

Perl .

(,% foo = (a = > 1, b = > 2)), , , json_decode - {a = > 1, b = > 2} ( ), (a = > 1, b = > 2) (, ).

:

Perl, . , - { "a": { "b": 3}}, { "b": 3} ; , .

( ), , , . JSON (= Perl), (= Perl). json_decode , , . JSON, , , ? ( % foo = json_decode (...), , .) , json_decode , , , .

, json_decode, .

#!/usr/bin/perl

use JSON qw (decode_json);
use Data::Dumper;
my $json = '["1", "2", "3", "4"]';

my $fromJSON = decode_json($json);

print Dumper($fromJSON);

$VAR1 = [ '1', '2', '3', '4' ];

ref, - ref

, , ?

,

my @array = @{ $fromJSON };

my @array = @{ $content->{'items'} }
0

, !

decode_json JSON .

, Perl,

use strict;
use warnings;

use JSON;

my $json_data = '{ "items": [ 1, 2, 3, 4 ] }';

my $content = decode_json($json_data);

use Data::Dump;
dd $content;

{ items => [1 .. 4] }

, $content -. , ,

dd $content->{items};

[1 .. 4]

,

print $content->{items}[0], "\n";

, ,

1

.

@cjm , , use strict use warnings Perl. , $content{items}, ,

Global symbol "%content" requires explicit package name

( ) , %content, items.

$content - %content, , $content{items}. %content , , items. @{$content->{items}}, , @{${$content}{items}}

If you really have a problem with the arrow operator, you can write

print ${$content}{items}[0], "\n";

which produces the same conclusion; but I don’t understand what is wrong with the original version.

0
source

All Articles