質問編集履歴
2
間違っている点、解読不能点にコメントを追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -4,7 +4,7 @@
|
|
4
4
|
|
5
5
|
#!/usr/bin/perl
|
6
6
|
|
7
|
-
print "Content-Type: text/html;
|
7
|
+
print "Content-Type: text/html; \n\n";
|
8
8
|
|
9
9
|
# use module
|
10
10
|
use Data::Dumper;
|
@@ -39,7 +39,7 @@
|
|
39
39
|
my $file_name = "message.json";
|
40
40
|
if(! -e $file_name) #ファイルがなければ、UTF8文字コードのjsonファイル作成
|
41
41
|
{
|
42
|
-
open my $fh,">
|
42
|
+
open my $fh,">", $file_name
|
43
43
|
or die "$file_name error $!";
|
44
44
|
close $fh;
|
45
45
|
}
|
@@ -47,9 +47,7 @@
|
|
47
47
|
my $dt = DateTime->now( time_zone=>'local');#現時刻取得
|
48
48
|
my $ymdhms = $dt->datetime;
|
49
49
|
|
50
|
-
|
51
|
-
|
52
|
-
my $json;
|
50
|
+
my $json; #jsonファイルから全てのメッセージデータを取得
|
53
51
|
{
|
54
52
|
local $/; #Enable 'slurp' mode
|
55
53
|
open my $fh,"<", $file_name;
|
@@ -58,11 +56,11 @@
|
|
58
56
|
}
|
59
57
|
|
60
58
|
|
61
|
-
my @array = ();
|
59
|
+
my @array = ();
|
62
60
|
|
63
61
|
if($json){
|
64
62
|
|
65
|
-
my $json_in = decode_json($json);
|
63
|
+
my $json_in = decode_json($json); ##新しいメッセージを現在のメッセージデータの最後に追加するために存在するデータをあらかじめ配列に入れておく(Jsonファイルに新しいデータを追加するのはどうやったらいいのかわかっていない....)
|
66
64
|
|
67
65
|
foreach my $item( @$json_in) {
|
68
66
|
push @array, $item;
|
@@ -70,6 +68,7 @@
|
|
70
68
|
}
|
71
69
|
}
|
72
70
|
|
71
|
+
#新メッセージ
|
73
72
|
my $new_data = {
|
74
73
|
'message' => $message,
|
75
74
|
'date' => $ymdhms,
|
@@ -77,9 +76,9 @@
|
|
77
76
|
};
|
78
77
|
|
79
78
|
|
80
|
-
push @array, $new_data;
|
79
|
+
push @array, $new_data;#過去のすべてのメッセージが入っている配列に追加
|
81
80
|
|
82
|
-
my $json_out = encode_json (\@array);
|
81
|
+
my $json_out = encode_json (\@array); #配列をJson化する
|
83
82
|
|
84
83
|
open my $fh, ">", $file_name
|
85
84
|
or die "$file_name error $!";
|
1
コードを変更しても直らないため、現在の現コードを書く
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,15 +1,92 @@
|
|
1
1
|
Perlで日本語をふくむデータをJson出力すると文字化けが起こります。
|
2
2
|
色々解決法をためしましたが、直りません。どこがおかしいんでしょうか?
|
3
|
+
```perl
|
3
4
|
|
5
|
+
#!/usr/bin/perl
|
6
|
+
|
7
|
+
print "Content-Type: text/html; charset=Shift_JIS\n\n";
|
8
|
+
|
9
|
+
# use module
|
10
|
+
use Data::Dumper;
|
11
|
+
use Time::Local;
|
12
|
+
use DateTime;
|
13
|
+
use JSON;
|
14
|
+
use utf8;
|
15
|
+
use Encode;
|
16
|
+
use URI::Escape;
|
17
|
+
|
18
|
+
use strict;
|
19
|
+
use warnings;
|
20
|
+
|
21
|
+
my $data;
|
22
|
+
#get data from unity
|
23
|
+
if ($ENV{'REQUEST_METHOD'} eq 'POST')
|
24
|
+
{
|
25
|
+
read(STDIN, $data, $ENV{'CONTENT_LENGTH'});
|
26
|
+
} #end of if
|
27
|
+
else
|
28
|
+
{
|
29
|
+
$data = $ENV{'QUERY_STRING'};
|
30
|
+
}#end of else
|
31
|
+
|
4
|
-
|
32
|
+
my $message;
|
33
|
+
my $client_number;
|
34
|
+
(my $message_tag,$message, my $client_number_tag, $client_number) = split(/=|&/,$data);
|
35
|
+
|
36
|
+
|
5
|
-
|
37
|
+
$message = uri_unescape($message);
|
38
|
+
|
39
|
+
my $file_name = "message.json";
|
40
|
+
if(! -e $file_name) #ファイルがなければ、UTF8文字コードのjsonファイル作成
|
41
|
+
{
|
42
|
+
open my $fh,">:encoding(UTF-8)", $file_name
|
43
|
+
or die "$file_name error $!";
|
44
|
+
close $fh;
|
45
|
+
}
|
46
|
+
|
47
|
+
my $dt = DateTime->now( time_zone=>'local');#現時刻取得
|
48
|
+
my $ymdhms = $dt->datetime;
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
my $json;
|
53
|
+
{
|
54
|
+
local $/; #Enable 'slurp' mode
|
55
|
+
open my $fh,"<", $file_name;
|
56
|
+
$json = <$fh>;
|
57
|
+
close $fh;
|
58
|
+
}
|
59
|
+
|
60
|
+
|
61
|
+
my @array = ();#アペンドするために存在するデータを配列に入れる
|
62
|
+
|
63
|
+
if($json){
|
64
|
+
|
65
|
+
my $json_in = decode_json($json);
|
66
|
+
|
67
|
+
foreach my $item( @$json_in) {
|
68
|
+
push @array, $item;
|
69
|
+
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
6
|
-
|
73
|
+
my $new_data = {
|
7
|
-
'id' => $last_id,
|
8
74
|
'message' => $message,
|
9
75
|
'date' => $ymdhms,
|
10
76
|
'client_number' => $client_number
|
11
77
|
};
|
12
78
|
|
79
|
+
|
13
80
|
push @array, $new_data;
|
14
|
-
|
81
|
+
|
15
|
-
my $json_out =
|
82
|
+
my $json_out = encode_json (\@array);
|
83
|
+
|
84
|
+
open my $fh, ">", $file_name
|
85
|
+
or die "$file_name error $!";
|
86
|
+
print $fh $json_out ;
|
87
|
+
close $fh;
|
88
|
+
|
89
|
+
print ("Success");
|
90
|
+
|
91
|
+
exit;
|
92
|
+
```
|