22 lines
457 B
Perl
Executable File
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"; |