2023-03-05 23:42:45
This commit is contained in:
22
src/json.pl
Executable file
22
src/json.pl
Executable file
@@ -0,0 +1,22 @@
|
||||
#! /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";
|
||||
Reference in New Issue
Block a user