Files
perl-examples/Writerside/topics/Json.md
2024-06-21 14:57:07 +09:00

884 B

JSON

sudo cpanm JSON

JSON - JSON (JavaScript Object Notation) encoder/decoder - metacpan.org

문자열을 해시로 변환

#! /usr/bin/perl
use strict;
use JSON;

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";

decode_json은 매개변수로 문자열을 전달받고, 변환 결과인 해시에 대한 참조를 반환합니다.

해시를 문자열로 변환

#! /usr/bin/perl
use strict;
use JSON;

my %person = ('name'=>'Steve', 'age'=>34);
my $text = &encode_json(\%person); # pass a reference to a hash
print "JSON TEXT: $text\n";

encode_json은 해시에 대한 참조를 매개변수로 전달 받고, 변환 결과를 문자열로 반환합니다.