我想將json文件轉換爲xml。因此,掃描JSON目錄,並且如果到達那裏的任何文件將被轉換爲xml並移動到xml目錄。Perl中格式不正確的JSON字符串
但是我正在此錯誤
的ReadLine()上閉合的文件句柄$跳頻在json.pl管線29
格式錯誤JSON字符串,既不陣列,對象,數字,字符串或原子,在字符(前 「(字符串的結束)」)在json.pl線34
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.js偏移0在
{
"Manager":
{
"Name" : "Mike",
"Age": 28,
"Hobbies": ["Music"]
},
"employees":
[
{
"Name" : "Helen",
"Age": 26,
"Hobbies": ["Movies", "Tennis"]
},
{
"Name" : "Rich",
"Age": 31,
"Hobbies": ["Football"]
}
]
}
爲了處理故障,在'decode_json'周圍放置一個'eval'會很好嗎? 'decode_json'發現錯誤。 –