我正在尝试使用JSON从数据库填充listView
。 listView
填充正常,但字符将替换为问号。我看了JSON。这些人物都是希腊文。
我的PHP代码:
<?php
try {
$handler = new PDO('mysql:host=localhost;dbname=database', 'root', 'password');
$handler->query( 'SET CHARACTER SET utf8' );
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
echo $e->getMessage();
die();
}
$query = $handler->query( 'SET CHARACTER SET utf8' );
$query = $handler->query('SELECT * FROM markoglou_order');
$records = array();
$records = $query->fetchAll(PDO::FETCH_ASSOC);
$json['categories_markoglou'] = $records;
echo json_encode($json, JSON_UNESCAPED_UNICODE);
?>
截图:
修改
我已尝试使用JSON_UNESCAPED_UNICODE
,但它无效。
答案 0 :(得分:2)
执行以下操作:
修改您的文件,如下所示:
<?php
mb_http_output("utf-8");
header("Content-type:application/json; charset=utf-8");
try {
$handler = new PDO('mysql:host=localhost;dbname=database', 'root', 'password');
$handler->query( 'SET CHARACTER SET utf8' );
$handler->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (Exception $e) {
echo $e->getMessage();
die();
}
$query = $handler->query( 'SET CHARACTER SET utf8' );
$query = $handler->query('SELECT * FROM markoglou_order');
$records = array();
$records = $query->fetchAll(PDO::FETCH_ASSOC);
$json['categories_markoglou'] = $records;
echo json_encode($json, JSON_UNESCAPED_UNICODE);
?>
然后你将得到希腊语所需的输出。
希望它有所帮助!!!