Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PERL中获得JSON输出,\n而不使用可读格式?_Json_Perl_Newline_Activeperl - Fatal编程技术网

如何在PERL中获得JSON输出,\n而不使用可读格式?

如何在PERL中获得JSON输出,\n而不使用可读格式?,json,perl,newline,activeperl,Json,Perl,Newline,Activeperl,我需要从PERL脚本中获取一个可读的JSON对象,但它不是可读的格式 这是生成JSON的代码 while (my ($orderID, $possessorName, $itemDescription, $customerPickUpTime, $customerDropOffTime, $paymentAmount, $originAddress1, $originAddress2, $originNeighborhood, $originZipCode, $desti

我需要从PERL脚本中获取一个可读的JSON对象,但它不是可读的格式

这是生成JSON的代码

            while (my ($orderID, $possessorName, $itemDescription, $customerPickUpTime, $customerDropOffTime, $paymentAmount, $originAddress1, $originAddress2, $originNeighborhood, $originZipCode, $destinationAddress1, $destinationAddress2, $destinationNeighborhood, $destinationZipCode) = $sth->fetchrow_array) 
        {
            %data = (orderID => $orderID, possessorName => $possessorName, itemDescription => $itemDescription, customerPickUpTime => $customerPickUpTime, customerDropOffTime => $customerDropOffTime, paymentAmount => $paymentAmount, originAddress1 => $originAddress1, originAddress2 => $originAddress2, originNeighborhood => $originNeighborhood, originZipCode => $originZipCode, destinationAddress1 => $destinationAddress1, destinationAddress2 => $destinationAddress2, destinationNeighborhood => $destinationNeighborhood, destinationZipCode => $destinationZipCode);


            $json_obj = JSON->new->allow_nonref;

            my $json_text = $json_obj->pretty->encode(\%data);

            $query_results{"job$index"} = {"data" => $json_text};

            $index++;

        }




        return $json_obj->pretty->encode(\%query_results, {ascii => 1, pretty => 1});
除了查看文件(这是打印行)外,其他一切都正常:

结果如下:

       {
    "job3" : {
  "data" : "{\n   \"originAddress1\" : \"101 East 105th Street\",\n   \"destinationZipCode\" : \"10128\",\n   \"destinationNeighborhood\" : \"Upper East Side\",\n   \"customerDropOffTime\" : \"2013-01-22 23:41:37\",\n   \"originAddress2\" : \"\",\n   \"paymentAmount\" : \"19.00\",\n   \"customerPickUpTime\" : \"2013-01-22 22:56:37\",\n   \"itemDescription\" : \"body\",\n   \"destinationAddress1\" : \"180 East 93rd Street\",\n   \"destinationAddress2\" : \"\",\n   \"possessorName\" : \"Lisa Howard\",\n   \"originZipCode\" : \"10029\",\n   \"originNeighborhood\" : \"East Harlem\",\n   \"orderID\" : \"723\"\n}\n"
},

JSON对象的格式正确,但问题在于
\n
。它不是用实际的换行符输出的。这就是问题所在

这是因为,
$json\u text
是一个字符串而不是散列。如果要将整个内容编码为JSON,则必须创建适当的数据结构

$query_results{"job$index"} = {"data" => \%data};
并将其作为一个整体提供给
encode

$query_results{"job$index"} = {"data" => \%data};