以下是我尝试做的类似示例:
@name = qw (Sam Tom John Mike Andrea);
@scores = qw (92 80 59 83 88);
我需要将这些数组存储为JavaScript,以便在网页上制作有用的图形。
答案 0 :(得分:2)
假设您希望将它们保留为单独的数组,请先在哈希中引用它们:
my %data = ( names => \@names, scores => \@scores );
然后使用JSON模块将数据结构序列化为JSON,例如:
use strict;
use warnings;
use JSON;
my @names = qw (Sam Tom John Mike Andrea);
my @scores = qw (92 80 59 83 88);
my %data = ( names => \@names, scores => \@scores );
my $json = encode_json \%data;
print $json
输出:
{"names":["Sam","Tom","John","Mike","Andrea"],"scores":["92","80","59","83","88"]}