JSON字符串中的特殊字符转义

JSON字符串中的特殊字符转义,json,perl,escaping,character,Json,Perl,Escaping,Character,我有一个Perl脚本,它包含变量$env->{'arguments'},这个变量应该包含一个JSON对象,我想将这个JSON对象作为参数传递给我的其他外部脚本,并使用backticks运行它 转义之前,$env->{'arguments'}的值: $VAR1 = '{"text":"This is from module and backslash \\ should work too"}'; $VAR1 = '"{\\"text\\":\\"This is from module and b

我有一个Perl脚本,它包含变量
$env->{'arguments'}
,这个变量应该包含一个JSON对象,我想将这个JSON对象作为参数传递给我的其他外部脚本,并使用backticks运行它

转义之前,
$env->{'arguments'}
的值:

$VAR1 = '{"text":"This is from module and backslash \\ should work too"}';
$VAR1 = '"{\\"text\\":\\"This is from module and backslash \\ should work too\\"}"';
转义后
$env->{'arguments'}
的值:

$VAR1 = '{"text":"This is from module and backslash \\ should work too"}';
$VAR1 = '"{\\"text\\":\\"This is from module and backslash \\ should work too\\"}"';
代码:

转义字符功能:

sub escapeCharacters
{
    #$env->{'arguments'} =~ s/\\/\\\\"/g;
    $env->{'arguments'} =~ s/"/\\"/g;
    $env->{'arguments'} = '"'.$env->{'arguments'}.'"';
}

我想问您什么是正确的方法,以及如何将JSON字符串解析为有效的JSON字符串,我可以将其用作脚本的参数。

将其作为管道传输到第二个程序,而不是作为参数传递,这似乎更有意义(而且更安全)

test1.pl

#!/usr/bin/perl

use strict;
use JSON;
use Data::Dumper;

undef $/;

my $data = decode_json(<>);
print Dumper($data);
#/usr/bin/perl
严格使用;
使用JSON;
使用数据::转储程序;
未定义$/;
my$data=decode_json();
打印转储程序(数据);
test2.pl

#!/usr/bin/perl

use strict;
use IPC::Open2;
use JSON;

my %data = ('text' => "this has a \\backslash", 'nums' => [0,1,2]);
my $json = JSON->new->encode(\%data);

my ($chld_out, $chld_in);
print("Executing script\n");
my $pid = open2($chld_out, $chld_in, "./test1.pl");
print $chld_in "$json\n";
close($chld_in);
my $out = do {local $/; <$chld_out>};
waitpid $pid, 0;
print(qq~test1.pl output =($out)~);
#/usr/bin/perl
严格使用;
使用IPC::Open2;
使用JSON;
我的%data=('text'=>“这有一个\\反斜杠,'nums'=>[0,1,2]);
我的$json=json->new->encode(\%data);
我的($chld_out,$chld_in);
打印(“执行脚本\n”);
my$pid=open2($chld_out,$chld_in,“./test1.pl”);
在“$json\n”中打印$chld_;
收盘价($chld_in);
my$out=do{local$/;};
waitpid$pid,0;
打印(qq~test1.pl输出=($out)~);
你正在重新创造一个新的世界

或者,您可以使用许多IPC::模块来代替
qx
。比如说,

use IPC::System::Simple qw( capturex );

my $output = capturex('./script.pl', $env->{arguments});
因为您至少有一个参数,所以还可以使用以下参数:

my $output = '';
open(my $pipe, '-|', './script.pl', $env->{arguments});
while (<$pipe>) {
   $output .= $_;
}

close($pipe);
替换

'./script.pl'


另见。另一种避免将参数交给shell(因此需要转义)的方法是将其写入tempfile,然后将tempfile的文件名作为参数传递给脚本,而不是传递给脚本。非常感谢,但我想真正使用参数,因为这样我就可以用docker命令调用这个脚本了。我试图把docker命令添加到shell中,结果得到:
cd/home/user/system/!project/_addons/Ext/PHPtest&&docker run--rm-v$(pwd):/app-w/app-php-php-hello.php
:通过将shell命令传递给
shell
?,无法找到您想要完成的具体任务?!?!它将生成
'cd/home/user/system/!project/_addons/Ext/PHPtest&&docker run--rm-v$(pwd):/app-w/app-php-php-hello.php'
,您显然尝试将其作为shell命令执行,该命令将尝试执行一个名为
cd/home/user/system/的文件!project/_addons/Ext/PHPtest&&docker run--rm-v$(pwd):/app-w/app-php-php-hello.php
。该文件“未找到”是轻描淡写的说法@我知道,但你们不明白我的意思,如果我只通过backsticks运行它,那个命令是有效的,文件是存在的,但我只是改变了它,我只把文件和参数包装到
shell中,它就工作了。不,毫无疑问我说的是正确的。没有名为
cd
的目录,更不用说路径
cd/home…
中的文件了。出现错误是因为您执行了shell命令“cd/home…”
,而不是shell命令“cd/home…”。也就是说,尽管我可以从你告诉我的一点信息中告诉你你做了什么以及为什么那是错误的,但我不知道你为什么这么做,或者你试图实现什么。但这里不是这样的地方。如果你有一个新问题,发布一个新问题。
'./script.pl'
"$RealBin/script.pl"