duanli0162 2016-11-09 20:19
浏览 90
已采纳

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

I got the following script that returns me the data of a mssql databse, it works fine however special caracters like "ç" "á" "ã", etc is displayed in a weird way.

From what i saw, as it is a mssql this can be a pain.. didnt find any exact response.

I tried to add this line in order to fix:

  • ini_set('mssql.charset', 'UTF-8');
  • "Database"=>"programaplo","UID"=>"--","PWD"=>"--","CharacterSet"=>"UTF-8");
  • header('Content-type: application/json; Charset=UTF-8');

The script

    <?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 
";

$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();
?>

The result:

{"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"}

The issue:

Associa\u00e7\u00e3o
  • 写回答

1条回答 默认 最新

  • dongmanni6916 2016-11-09 20:24
    关注

    Associa\u00e7\u00e3o is the official way to encode binary (or non-ASCII) characters in JSON. Every Json parser [can|should] understand this notation.

    See : JSON and escaping characters

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上