我正在尝试创建一个显示netapp文件管理器使用情况的图表的网页。
我从文件中读取数据,该数据以下列方式存储
Name usedSpace available_space kbytes quota_volume capacity
Juno 889347800 1795006760 2684354560 1698693120 33%
这是我的Perl代码
use CGI;
use Time::Local;
use List::MoreUtils qw(uniq);
use GD;
use GD::Graph::bars;
use GD::Graph::lines;
use GD::Graph::area;
open (MYFILE, 'my_data.csv');
@filesystem_name = ();
@my_date = ();
@my_used = ();
@my_avail = ();
@my_kbytes = ();
@my_quota_volume = ();
@my_capacity = ();
#read all the data and store fields into arrays
while (<MYFILE>) {
if(/$user_selected_filer/) {
($dummy,$current_line_date,$dummy,$dummy,$dummy,$dummy,$dummy) = split (/,/, $_);
if(($current_line_timestamp ge $start && $current_line_timestamp le $end)){
($name,$date_after,$used,$avail, $kbytes, $quota_volume, $capacity) = split (/,/, $_);
($month_after,$day_of_month_after,$year_after) = split ('/', $date_after);
push (@filesystem_name,$name);
push (@my_used,$used/1000000);
push (@my_avail,$avail/1000000);
push (@my_kbytes,$kbytes/1000000);
push (@my_quota_volume,$quota_volume/1000000);
push (@my_capacity,$capacity);
push (@my_date_after, $month_after . "/" . $day_of_month_after . "/" . $year_after);
$count++;
}
}
}
只要我使用GD图形,这些数组就可以使用Perl制作图形。但是我想在需要JavaScript数组的网页上做这些图。
我对这整个JSON事情感到很失望。
答案 0 :(得分:0)
因为不知道你真正想要什么,这里有一个在线人员:
perl -MJSON::XS -MText::CSV::Slurp -E 'say JSON::XS->new->utf8->encode(Text::CSV::Slurp->load(filehandle => *STDIN))' < file.csv
从这些数据:
Name,usedSpace,available_space,kbytes,quota_volume,capacity
Juno,889347800,1795006760,2684354560,1698693120,33%
Juno2,89347800,795006760,684354560,698693120,3%
Juno3,9347800,95006760,84354560,98693120,1%
创建:
[{"usedSpace":"889347800","quota_volume":"1698693120","kbytes":"2684354560","available_space":"1795006760","capacity":"33%","Name":"Juno"},{"usedSpace":"89347800","quota_volume":"698693120","kbytes":"684354560","available_space":"795006760","capacity":"3%","Name":"Juno2"},{"usedSpace":"9347800","quota_volume":"98693120","kbytes":"84354560","available_space":"95006760","capacity":"1%","Name":"Juno3"}]
或者更漂亮,这个:
perl -MJSON::XS -MText::CSV::Slurp -E 'say JSON::XS->new->utf8->pretty->encode(Text::CSV::Slurp->load(filehandle => *STDIN))' < file.csv
创建:
[
{
"usedSpace" : "889347800",
"quota_volume" : "1698693120",
"kbytes" : "2684354560",
"available_space" : "1795006760",
"capacity" : "33%",
"Name" : "Juno"
},
{
"usedSpace" : "89347800",
"quota_volume" : "698693120",
"kbytes" : "684354560",
"available_space" : "795006760",
"capacity" : "3%",
"Name" : "Juno2"
},
{
"usedSpace" : "9347800",
"quota_volume" : "98693120",
"kbytes" : "84354560",
"available_space" : "95006760",
"capacity" : "1%",
"Name" : "Juno3"
}
]
答案 1 :(得分:0)
我不确定你的问题是什么,但这些想法可能会有所帮助
JSON数据是JavaScript对象和数组的独立于语言的限制。
JSON是一种使用简单字符串表示数据结构的方法。
由于哈希和数组的思想很常见,JSON可用于在不同语言和不同平台之间移动信息。
从您的问题中不清楚您要移动哪些数据,以及在哪里移动。但是你可以使用JSON
模块的encode_json
方法从Perl创建一个JSON字符串,它会为你提供一个你可以通过任何方式发送到任何地方的字符串。
相应地,您可以使用JSON.parse
构建一个镜像原始Perl数据的JavaScript对象。