作者热门文章
- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
因为我不能使用 JSON_UNESCAPED_UNICODE
因为我的 php 是 < 5.4,我尝试了一些来自 json_encode()
manual page 的替代品 | :
$final = array (
0 => array (
'id' => 26629,
'content' => 'преди 5 сек'
),
1 => array (
'id' => 26628,
'content' => 'преди 5 сек'
),
2 => array (
'id' => 26627,
'content' => 'преди 5 сек'
)
);
$myDirtyString = json_encode($final);
$myDirtyString = str_replace("\/","/",$myDirtyString);
$myDirtyString = str_replace('"','\\"',$myDirtyString);
echo $myCleanedString = json_decode('"'.$myDirtyString.'"');
结果是:
[
{"id":"26629","timestamp":"преди 5 сек"},
{"id":"26628","timestamp":"преди 5 сек"},
{"id":"26625","timestamp":"¿Ñеди 5 Ñек"}
]
为什么最后一项这么乱?
我试过了
header('Content-Type: application/json; charset=utf-8');
...但仍然没有工作。
最佳答案
如果您使用的是 5.4 之前的 PHP 版本,这可能会有所帮助:
<?php
if (!function_exists('json_encode')) {
function json_encode($data) {
switch ($type = gettype($data)) {
case 'NULL':
return 'null';
case 'boolean':
return ($data ? 'true' : 'false');
case 'integer':
case 'double':
case 'float':
return $data;
case 'string':
return '"' . addslashes($data) . '"';
case 'object':
$data = get_object_vars($data);
case 'array':
$output_index_count = 0;
$output_indexed = array();
$output_associative = array();
foreach ($data as $key => $value) {
$output_indexed[] = json_encode($value);
$output_associative[] = json_encode($key) . ':' . json_encode($value);
if ($output_index_count !== NULL && $output_index_count++ !== $key) {
$output_index_count = NULL;
}
}
if ($output_index_count !== NULL) {
return '[' . implode(',', $output_indexed) . ']';
} else {
return '{' . implode(',', $output_associative) . '}';
}
default:
return ''; // Not supported
}
}
}
?>
关于php - json 编码和替换 JSON_UNESCAPED_UNICODE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9802033/
代码如下: <?php echo json_encode("中文"); //"\u4e2d\u6587" 这就让我们这些
因为我不能使用 JSON_UNESCAPED_UNICODE 因为我的 php 是 array ( 'id' => 26629, 'content' => 'преди 5 сек'
我正在从数据库中选择一些数据并将它们编码为 json,但我遇到了像 这样的捷克符号的问题 á,í,ř,č,ž... 我的文件是 utf-8 编码,我的数据库也是 utf-8 编码,我也将 header
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
当您有一个数组字段并将其保存在数据库中时,它会对数组执行漂亮的 json_encode 但没有 JSON_UNESCAPED_UNICODE 选项。数据最终是这样的: {"zh":"\u039d\u0
我是一名优秀的程序员,十分优秀!