来自 MariaDB、Drupal、php 的 UTF-8 字符
UTF-8 characters from MariaDB, Drupal, php
所以我有 MARIADB 集群数据库,其中我的字符集设置为:utf8_general_ci
我在 Drupal 模块中也有 PHP 函数 DB_Selects 行,最后将它们作为 "JSON" 返回。
问题是它returns UTF-8编码的字符,但是应该解码..
示例:Ü 返回为:\u00dc
php 发挥我所拥有的
function jgid_response($field, $id) {
drupal_add_http_header("Expires", -1);
drupal_page_is_cacheable(FALSE);
$res = db_select('TABLE1', 'ur')
->fields('ur', array('Cust_id', 'User_id'));
$res->addExpression('IF(begin > 0, DATE_FORMAT(FROM_UNIXTIME(begin), \'%d.%m.%Y\'), 0)', 'begin');
$res->addExpression('IF(end > 0, DATE_FORMAT(FROM_UNIXTIME(end), \'%d.%m.%Y\'), 0)', 'end');
$res->join('TABLE2', 'PERSON', 'person.id = ur.Cust_id');
$res->join('TABLE2', 'COMPANY', 'company.id = ur.User_id');
$res->fields('person', array('firstName', 'lastName'));
$res->fields('company', array('name'));
$res->condition($field, $id)
->condition('ur.notActive', 0);
$string = (string) $res;
$res = $res->execute();
if ($res->rowCount() > 0) {
$data = array();
while ($result = $res->fetchObject()) {
$result->application = 'APPLICATION_NAME';
$data[] = $result;
}
return array(
'result' => 'OK',
'list' => $data,
);
}
}
有什么方法可以强制该数组编码为字符吗?
我试过:
JSON_PRETTY_PRINT、JSON_UNESCAPED_SLASHES 以及 JSON_UNESCAPED_UNICODE
还尝试添加:
drupal_add_http_header("X-JSON-RESPONSE", 1);
drupal_add_http_header('Content-Type', 'text/csv; utf-8');
我哪里错了?
带有转义 Unicode 字符的 JSON 有效。您没有告诉我们您的 php 程序在哪里序列化 JSON。 built-in php json_encode() 方法以这种方式生成它。
JSON.parse()
在您的浏览器中应该返回具有正确 unicode 表示的数据。
请注意,您不能影响使用 Content-Type 或其他 headers 编码 JSON 的方式。为什么不? JSON 本身决定了它的内部格式。
由于其在 Unicode 诞生之前的历史,php 通常会尽可能使用 /u
样式编码。
所以我有 MARIADB 集群数据库,其中我的字符集设置为:utf8_general_ci
我在 Drupal 模块中也有 PHP 函数 DB_Selects 行,最后将它们作为 "JSON" 返回。
问题是它returns UTF-8编码的字符,但是应该解码..
示例:Ü 返回为:\u00dc
php 发挥我所拥有的
function jgid_response($field, $id) {
drupal_add_http_header("Expires", -1);
drupal_page_is_cacheable(FALSE);
$res = db_select('TABLE1', 'ur')
->fields('ur', array('Cust_id', 'User_id'));
$res->addExpression('IF(begin > 0, DATE_FORMAT(FROM_UNIXTIME(begin), \'%d.%m.%Y\'), 0)', 'begin');
$res->addExpression('IF(end > 0, DATE_FORMAT(FROM_UNIXTIME(end), \'%d.%m.%Y\'), 0)', 'end');
$res->join('TABLE2', 'PERSON', 'person.id = ur.Cust_id');
$res->join('TABLE2', 'COMPANY', 'company.id = ur.User_id');
$res->fields('person', array('firstName', 'lastName'));
$res->fields('company', array('name'));
$res->condition($field, $id)
->condition('ur.notActive', 0);
$string = (string) $res;
$res = $res->execute();
if ($res->rowCount() > 0) {
$data = array();
while ($result = $res->fetchObject()) {
$result->application = 'APPLICATION_NAME';
$data[] = $result;
}
return array(
'result' => 'OK',
'list' => $data,
);
}
}
有什么方法可以强制该数组编码为字符吗? 我试过: JSON_PRETTY_PRINT、JSON_UNESCAPED_SLASHES 以及 JSON_UNESCAPED_UNICODE
还尝试添加:
drupal_add_http_header("X-JSON-RESPONSE", 1);
drupal_add_http_header('Content-Type', 'text/csv; utf-8');
我哪里错了?
JSON 有效。您没有告诉我们您的 php 程序在哪里序列化 JSON。 built-in php json_encode() 方法以这种方式生成它。
JSON.parse()
在您的浏览器中应该返回具有正确 unicode 表示的数据。
请注意,您不能影响使用 Content-Type 或其他 headers 编码 JSON 的方式。为什么不? JSON 本身决定了它的内部格式。
由于其在 Unicode 诞生之前的历史,php 通常会尽可能使用 /u
样式编码。