Files
perl-examples/src/json.pl
2023-03-05 23:42:45 +09:00

22 lines
457 B
Perl
Executable File

#! /usr/bin/perl
use strict;
use JSON; # https://metacpan.org/pod/JSON
# sudo cpanm JSON
## from json to hash
my $json = <<JSON;
{
"name":"charlie",
"age":13
}
JSON
my $obj = &decode_json($json); # returns a reference to a hash
print "NAME: $obj->{'name'}\n";
print "AGE: $obj->{'age'}\n";
## from hash to json
my %person = ('name'=>'Steve', 'age'=>34);
my $text = &encode_json(\%person); # pass a reference to a hash
print "JSON TEXT: $text\n";