perl で JSON データを出力するメモ。
perl モジュール JSON を使用。
#!/usr/bin/perl use strict; use warnings; use JSON; my @h_arr; my @array = ( 'a', 'b', 'c' ); my $hash1 = { 'a' => '1', 'b' => '2', 'c' => '3' }; my $hash2 = { 'a' => [@array], 'b' => [@array], 'c' => 3 }; my $hash3 = { 'a' => $hash1 }; for ( 1..3 ) { push( @harr, [@arr] ); } &json( '$hash1', $hash1 ); &json( '$hash2', $hash2 ); &json( '$hash3', $hash3 ); &json( '@array', \@array ); &json( '@_harr', \@h_arr ); sub json { my $data = shift; my $json; $json = to_json($data, { utf8 => 1}); print "$json\n"; return; } exit;
これを実行するとこんな感じ。
$hash1 = {"c":"3","a":"1","b":"2"} $hash2 = {"c":3,"a":["a","b","c"],"b":["a","b","c"]} $hash3 = {"a":{"c":"3","a":"1","b":"2"}} @array = ["a","b","c"] @_harr = [["a","b","c"],["a","b","c"],["a","b","c"]]
簡単にデータを JSON データに変換できるので便利。