使用Perl解析JSON的麻烦

时间:2011-02-21 01:31:27

标签: perl json parsing

我有一个JSON文件,我试图在Perl中解析它。到目前为止,我有:

use strict;
use warnings;
use JSON;

open my $fh, "/Users/arjunnayini/Desktop/map_data.json";   


my @decoded_json = @{decode_json($fh)};

但是我收到的错误是: “格式错误的JSON字符串,无论是数组,对象,数字,字符串还是原子,在字符偏移0处(在”GLOB(0x100804ed0)“之前)”

我很确定JSON文件格式正确,所以我不确定这是哪里出错的。有什么建议吗?

1 个答案:

答案 0 :(得分:15)

假设您对JSON的调用是正确的,您需要首先将文件粘贴:

#!/usr/bin/perl

use strict;
use warnings;
use JSON;

my $json;
{
  local $/; #enable slurp
  open my $fh, "<", "/Users/arjunnayini/Desktop/map_data.json";
  $json = <$fh>;
} 

my @decoded_json = @{decode_json($json)};
相关问题