【问题标题】:malformed JSON string in PerlPerl 中格式错误的 JSON 字符串
【发布时间】:2015-03-07 03:48:11
【问题描述】:

我正在尝试将 json 文件转换为 xml。因此 JSON 目录被扫描,如果有任何文件到达那里将被转换为 xml 并移动到 xml 目录。

但是我收到了这个错误

在 json.pl 第 29 行关闭文件句柄 $fh 上的 readline()。
格式错误的 JSON 字符串,既不是数组、对象、数字、字符串也不是原子,在 json.pl 第 34 行的字符偏移量 0(“(字符串结尾)”之前)

json.pl

#!/usr/bin/perl

use strict;
use warnings;
use File::Copy;

binmode STDOUT, ":utf8";
use utf8;

use JSON;
use XML::Simple;

# Define input and output directories
my $indir = 'json';
my $outdir = 'xml';

# Read input directory
opendir DIR, $indir or die "Failed to open $indir";
my @files = readdir(DIR);
closedir DIR;

# Read input file in json format
for my $file (@files) 
{
my $json;
{
    local $/; #Enable 'slurp' mode
    open my $fh, "<", "$indir/$file";
    $json = <$fh>;
    close $fh;
}

# Convert JSON format to perl structures
my $data = decode_json($json);

# Output as XML
open OUTPUT, '>', "$outdir/$file" or die "Can't create filehandle: $!";
select OUTPUT; $| = 1;
print "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
print XMLout($data);
print "\n" ;
close(OUTPUT);
unlink "$indir/$file";

}

example.json

{
"Manager":
    {
        "Name" : "Mike",
        "Age": 28,
        "Hobbies": ["Music"]
     },
"employees":
    [
        {
            "Name" : "Helen",
            "Age": 26,
            "Hobbies": ["Movies", "Tennis"]
           },
        {
            "Name" : "Rich",
            "Age": 31,
            "Hobbies": ["Football"]

        }
    ]
}

【问题讨论】:

    标签: xml json perl malformed


    【解决方案1】:

    您没有在open 期间检查错误,也没有跳过目录条目(readdir 将返回 ... 条目)。

    如果你使用

    open my $fh, "<", "$indir/$file" or die "$file: $!";
    

    您可能很快就会发现问题。

    “已关闭文件句柄 $fh 上的 readline()”表示“open $fh 失败,但你还是继续前进”。

    【讨论】:

      【解决方案2】:

      正如@cjm 所指出的,问题在于您正试图打开和读取目​​录以及源目录中的文件。

      这是一个修复,它还使用autodie 来避免不断检查所有 IO 操作的状态。我也整理了一下。

      #!/usr/bin/perl
      
      use utf8;
      use strict;
      use warnings;
      use autodie;
      
      use open qw/ :std :encoding(utf8) /;
      
      use JSON qw/ decode_json /;
      use XML::Simple qw/ XMLout /;
      
      my ($indir, $outdir) = qw/ json xml /;
      
      my @indir = do {
        opendir my $dh, $indir;
        readdir $dh;
      };
      
      for my $file (@indir) {
      
        my $infile = "$indir/$file";
        next unless -f $infile;
      
        my $json = do {
          open my $fh, '<', $infile;
          local $/;
          <$fh>;
        };
      
        my $data = decode_json($json);
      
        my $outfile = "$outdir/$file";
        open my $out_fh, '>', "$outdir/$file";
        print $out_fh '<?xml version="1.0" encoding="UTF-8" standalone="yes"?>', "\n";
        print $out_fh XMLout($data), "\n";
        close $out_fh;
      
        unlink $infile;
      }
      

      【讨论】:

      • decode_json 周围放置一个eval 以处理故障会更好吗? decode_json 抱怨错误。
      猜你喜欢
      • 2016-03-16
      • 1970-01-01
      • 2014-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-14
      • 1970-01-01
      相关资源
      最近更新 更多