Perl JSON模块|无法获取JSON响应

Perl JSON模块|无法获取JSON响应,perl,json,Perl,Json,我有一个用于填充JQgrid对象的脚本 #!/usr/bin/perl use CGI; use DBI; use JSON; use Test::JSON; use POSIX qw(ceil); use strict; use warnings; my $cgi = CGI->new; my $page = $cgi->param('page'); my $limit = $cgi->param('limit'); my $sidx = $cgi->param('

我有一个用于填充JQgrid对象的脚本

#!/usr/bin/perl 
use CGI;
use DBI;
use JSON;
use Test::JSON;
use POSIX qw(ceil);
use strict;
use warnings;

my $cgi = CGI->new;
my $page = $cgi->param('page');
my $limit = $cgi->param('limit');
my $sidx = $cgi->param('sidx');
my $sordx = $cgi->param('sordx');

if(!$sidx) {$sidx = 1};
my $start = $limit*$page - $limit;

my $dbh = DBI->connect('dbi:mysql:hostname=localhost;database=test',"test","test") or die $DBI::errstr;
my $count = $dbh->selectrow_array("SELECT COUNT(id) AS count FROM test;");
my $sql = "SELECT ID, Name FROM test ORDER BY ? ? LIMIT ?, ?;";
my $sth = $dbh->prepare($sql) or die $dbh->errstr;

$sth->execute($sidx,$sordx,$start,$limit) or die $sth->errstr;
my $total_pages;
if( $count >0 ) {
        $total_pages = ceil($count/$limit);
} else {
        $total_pages = 0;
}
if ($page > $total_pages){ $page=$total_pages};
$start = $limit*$page - $limit;
my $i=0;
my $response = {};
$response->{page} = $page;
$response->{total} = $total_pages;
$response->{records} = $count;
        while (my $row = $sth->fetchrow_arrayref) {
                my @arr = @{$row};
                $response->{rows}[$i]{id} = $row->[0];
                $response->{rows}[$i]{cell} = \@arr;
                $i++;
        }

Test::JSON::is_valid_json $response, '... json is well formed';

print $cgi->header(-type => "application/json", -charset => "utf-8");
print JSON::to_json($response,{ ascii => 1, pretty => 1 });
如果我将脚本置于CGI调试模式,它将返回以下内容(注意,手动编辑以屏蔽敏感数据):

编辑:我添加了Test::JSON包,它给出了以下错误:

输入无效JSON:格式不正确 JSON字符串,既不是数组,也不是对象, 字符处的数字、字符串或原子 偏移量0(在“(字符串结尾)”之前) /usr/lib/perl5/site_perl/5.8.8/JSON/Any.pm 第571行


我不知道下一步该怎么做。我对代码所做的任何其他更改都不会将适当的括号放在返回的字符串上。

将此字符串传递到
JSON::decode
,我看到对字符串中的文字换行符的抱怨
“这是记录1”
“这是记录2”
。如果将它们转换为
\n
,会怎么样

foreach my $row ( @{$ref} ) {
    $response->{rows}[$i]{id} = @$row[0];
    $response->{rows}[$i]{cell} = $row;

    # escape newlines in database output
    s/\n/\\n/g for @{$response->{rows}[$i]{cell}};

    $i++;
}

(可能有一种更通用、更健壮的方法可以做到这一点)

我认为新行是在他粘贴JSON时添加的。<代码> tojJSON/COD>函数应该自动执行任何必要的转义。但是,JSON字符串应该有文字<代码> \n>代码> s,而不是引号表达式中间的换行符。什么时候可以进行插值?要么(1)JSON::to_JSON一开始没有这样做,(2)CGI记录器更改了它,(3)OP在键入问题时更改了它,要么(4)在OP的剪贴板上复制和粘贴到SO之间更改了它。无论哪种方式,发布的都不是“有效的JSON结果”。我相当肯定它们一定是在2、3或4中添加的,因为
to_JSON
不应该生成无效的JSON。所以你的答案不能解决问题,因为他没有解释问题是什么。简化你的程序以帮助调试。编码这个文本数据结构是否如预期的那样工作<代码>打印JSON::to_JSON({page=>unde,records=>25,rows=>[{cell=>[1,1999-01-01],(“0.00”)x 3,“带标点符号的注释”],id=>1},{cell=>[2,2001-01-10],103.98,45.34,149.32,“这是记录1”],id=>2},{cell=>[3,2001-02-10],104.98,46.34,151.32,“这是记录2”],id=>3},总计=>0},当然,
Test::JSON::is_valid_JSON$response
告诉您它不是有效的JSON。此时,
$response
是编码为JSON之前的hashref。
foreach my $row ( @{$ref} ) {
    $response->{rows}[$i]{id} = @$row[0];
    $response->{rows}[$i]{cell} = $row;

    # escape newlines in database output
    s/\n/\\n/g for @{$response->{rows}[$i]{cell}};

    $i++;
}