1

我能够将硬编码的 json 字符串转换为 perl 哈希,但是如果我想将完整的 json 文件转换为 perl 数据结构,以后可以以任何方式解析,我会收到以下错误。格式错误的 JSON 字符串,既不是数组、对象、数字、字符串也不是原子,在 json_vellai.pl 第 9 行的字符偏移量 0(“(字符串结尾)”之前)

use JSON::PP;
$json= JSON::PP->new()

$json = $json->allow_singlequote([$enable]);

open (FH, "jsonsample.doc") or die "could not open the file\n";

#$fileContents = do { local $/;<FH>};

@fileContents = <FH>;

#print @fileContents;

$str = $json->allow_barekey->decode(@filecontents);

foreach $t (keys %$str)

{


print "\n $t -- $str->{$t}";

}

这就是我的代码的样子..请帮帮我

4

1 回答 1

2

在我看来,decode它不想要一个列表,它想要一个标量字符串。

你可以 slurp 文件:

undef $/;
$fileContents = <FH>;
于 2011-08-22T06:56:27.700 回答