さて、消費税の10%増税が本日10月1日から開始ですね。
筆者は庶民的なエンジニアなので9月より弁当男子、マイボトル男子となりましたが、今後は家庭菜園男子としても活動していこうと思います。
家庭菜園とIOTも組み合わせて何かしら仕掛けを作ってみたいですね。

今回も各種言語と言いながらperlとphpについての記事となります。
他の言語についても後日更新かけていきたいと思います。

perl

JSONデータ作成

#!/usr/bin/perl
use utf8;
use JSON;
use warnings;

my $data=[
        {No=>'1',Name=>'Yamamoto Taro',Seitou=>'Reiwa Shinsengumi'},
        {No=>'2',Name=>'Funago Yasuhiko',Seitou=>'Reiwa Shinsengumi'},
        {No=>'3',Name=>'Kimura Eiko',Seitou=>'Reiwa Shinsengumi'},
];

my $json=JSON->new();
my $js=$json->encode($data);

print $js,"\n";

実行結果

[{"Name":"Yamamoto Taro","Seitou":"Reiwa Shinsengumi","No":"1"},{"Name":"Funago Yasuhiko","No":"2","Seitou":"Reiwa Shinsengumi"},{"Seitou":"Reiwa Shinsengumi","No":"3","Name":"Kimura Eiko"}]

JSONデータの読み込み

#!/usr/bin/perl
use utf8;
use JSON;
use warnings;

my $json_data='[{"No":"1","Name":"Yamamoto Taro","Seitou":"Reiwa Shinsengumi"},{"No":"2","Seitou":"Reiwa Shinsengumi","Name":"Funago Yasuhiko"},{"No":"3","Seitou":"Reiwa Shinsengumi","Name":"Kimura Eiko"}]';

my $items=JSON->new()->decode($json_data);
foreach my $item(@$items){
        printf "%s,%s,%s\n",$item->{No},$item->{Name},$item->{Seitou};
}

実行結果

1,Yamamoto Taro,Reiwa Shinsengumi
2,Funago Yasuhiko,Reiwa Shinsengumi
3,Kimura Eiko,Reiwa Shinsengumi

php

JSONデータ作成

<?php
$data=array(
        array('No'=>'1','Name'=>'Yamamoto Taro','Seitou'=>'Reiwa Shinsengumi'),
        array('No'=>'2','Name'=>'Funago Yasuhiko','Seitou'=>'Reiwa Shinsengumi'),
        array('No'=>'3','Name'=>'Kimura Eiko','Seitou'=>'Reiwa Shinsengumi'),
);
$json = json_encode($data);
print($json);
?>

実行結果

[{"No":"1","Name":"Yamamoto Taro","Seitou":"Reiwa Shinsengumi"},{"No":"2","Name":"Funago Yasuhiko","Seitou":"Reiwa Shinsengumi"},{"No":"3","Name":"Kimura Eiko","Seitou":"Reiwa Shinsengumi"}]

JSONデータ読み込み

<?php
$array=json_decode('[{"Name":"Yamamoto Taro","Seitou":"Reiwa Shinsengumi","No":"1"},{"Name":"Funago Yasuhiko","No":"2","Seitou":"Reiwa Shinsengumi"},{"Seitou":"Reiwa Shinsengumi","No":"3","Name":"Kimura Eiko"}]',true);
print_r($array);
?>

実行結果

Array
(
    [0] => Array
        (
            [Name] => Yamamoto Taro
            [Seitou] => Reiwa Shinsengumi
            [No] => 1
        )

    [1] => Array
        (
            [Name] => Funago Yasuhiko
            [No] => 2
            [Seitou] => Reiwa Shinsengumi
        )

    [2] => Array
        (
            [Seitou] => Reiwa Shinsengumi
            [No] => 3
            [Name] => Kimura Eiko
        )

)