【发布时间】:2020-08-25 08:47:51
【问题描述】:
我正在尝试从 FitBit 加载如下所示的健身数据:
[{
"dateTime" : "03/16/19 05:02:00",
"value" : "0"
},{
"dateTime" : "03/16/19 05:05:00",
"value" : "0"
},{
... about 64,800 lines here
},{
"dateTime" : "04/15/19 04:47:00",
"value" : "0"
},{
"dateTime" : "04/15/19 04:58:00",
"value" : "0"
}]
我尝试使用 JSON 包将其读入 Perl,如下所示:
sub json_file_to_hash {
my $file = shift;
open my $in, '<', $file;
my $json = <$in>;
close $in;
$json = "$json\n";
my $ref = decode_json $json;
return %{ $ref }
}
当我在 Perl 上运行它时,我得到了这个:
'"' expected, at character offset 4 (before "(end of string)") at fitbit.pl line 15.
这个错误没有任何意义。它使字符串看起来好像没有被字符串包围。
如何使用 JSON 包将此文件加载到哈希中?
【问题讨论】:
-
提示:习惯使用参考。您不应该返回散列的各个键和值以在调用者中从它们构建新的散列,而不是对散列的引用。不好:
return %{ $ref }。好:return $ref