如何在Json对象中保留换行符

时间:2017-07-19 18:16:14

标签: php sql-server json curl

在linux上的php代码中,我将一个带有Json对象的CURL请求提交给了webservice。

字符串基于JSON对象中的64编码。然后,Web服务将收到的字符串放入mssql数据库。

在数据库中,我发现\n(新行)已被替换。我想知道如何将字符串的换行符保存到mssql数据库中,以便稍后检索它时,会显示换行符。

我尝试将\n替换为\\n,但它无效。 \\n在字符串中显示为\。我也尝试用<br />替换。它也没用。从mssql中检索时,<br />字面上显示在字符串中而不是换行符。

以下是代码:

$url = "https://<some website>/DocumentService/".base64_encode("#".$enmPatId)."/Document";
echo $url.'<br>';
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//$fileName = "QAChartRpt.txt";
$fileName = $enmPatId.".txt";
echo "fileName:$fileName";
$fileContent = file_get_contents($fileName);

//the following $fileContent has the newline in it
$base64= base64_encode($fileContent);
$postData = array (
                'BinaryContent' => $base64,
                'FileFormat' => $docType,
                'DateOfService' => $currentT,
                'TemplateName' => 'Notes',
                'DocumentType' =>'#1139'
            );

curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
$res2= curl_exec($ch);
if($res2 != false) {
    //var_dump($res2);
    $response2= json_decode($res2,true);
    print_r($response2);
}

0 个答案:

没有答案