使用来自mssql数据库的数据在json中使用charset的问题

时间:2016-11-09 20:19:50

标签: php sql-server json utf-8

我得到了以下脚本,它返回了mssql数据库的数据,它工作正常,但是“甓ᔓã”等特殊字符以奇怪的方式显示。

从我所看到的,因为它是一个mssql,这可能是一个痛苦..没有找到任何确切的回应。

我尝试添加此行以修复:

  • ini_set('mssql.charset','UTF-8');
  • “数据库”=> “中programaplo”, “UID”=> “中 - ”, “PWD”=> “中 - ”, “字符集”=> “中UTF-8”);
  • header('Content-type:application / json; Charset = UTF-8');

脚本

    <?php
error_reporting(1);
ini_set('mssql.charset', 'UTF-8');

$serverName = "mssql3.gear.host";

/* Get UID and PWD from application-specific files.  */
$connectionInfo = array( "Database"=>"programaplo", "UID"=>"--", "PWD"=>"--","CharacterSet"=>"UTF-8");
$conn = sqlsrv_connect($serverName, $connectionInfo);

if( $conn ) {
     echo "Connection established.<br />";
}else{
     echo "Connection could not be established.<br />";
     die( print_r( sqlsrv_errors(), true));
}

$tsql = "SELECT * FROM Obras";
$stmt = sqlsrv_query($conn, $tsql);

if( $stmt === false ) {
     echo "Error in executing query.</br>";
     die( print_r( sqlsrv_errors(), true));
}

echo "Query: sucesso \n";

$json = array();

while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
        $json[] = $row;
     }

header('Content-type: application/json; Charset=UTF-8');
echo json_encode($json);

sqlsrv_free_stmt($stmt);
sqlsrv_close($conn); //Close the connnectiokn first

exit();
?>

结果:

{"Id":2,"NomeObra":"Super Olimpia","idCliente":"Associa\u00e7\u00e3o Super Olimpia","DataPLevantamento":"4 de agosto de 2016","DataRLevantamento":"4 de agosto de 2016","Estado":"Obra conclu\u00edda ","DataRMateriais":"6 de setembro de 2016","DataInicioObra":"18 de setembro de 2016","DataConclusao":"20 de outubro de 2016","DataVestoria":"18 de setembro de 2016","Obs":"","Prompor":"Gas Fenosa","Levantpor":"Ploran\/Paulo","executpor":"Ploran"}

问题:

Associa\u00e7\u00e3o

1 个答案:

答案 0 :(得分:1)

Associa\u00e7\u00e3o是在JSON中编码二进制(或非ASCII)字符的官方方式。 每个Json解析器[都可以]理解这种表示法。

请参阅:JSON and escaping characters