使用Perl解析JSON的麻烦

Arj*_*ini 4 perl parsing json

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

use strict;
use warnings;
use JSON;

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


my @decoded_json = @{decode_json($fh)};
Run Code Online (Sandbox Code Playgroud)

但我得到一个错误,我有一个:"格式错误的JSON字符串,无论是数组,对象,数字,字符串还是原子,字符偏移0(在"GLOB(0x100804ed0)"之前)"

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

Joe*_*ger 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)};
Run Code Online (Sandbox Code Playgroud)

  • 或decode_json(File :: Slurp :: read_file('/ Users/ar ...')) (4认同)