In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
这篇文章主要介绍"怎么解决php5.4以下版本json不支持不转义内容中文的问题",在日常操作中,相信很多人在怎么解决php5.4以下版本json不支持不转义内容中文的问题问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答"怎么解决php5.4以下版本json不支持不转义内容中文的问题"的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
本文实例讲述了php5.4以下版本json不支持不转义内容中文的解决方法。分享给大家供大家参考。具体分析如下:
写ERP接口的时候遇到JAVA方接收这种json_encode后的内容
复制代码 代码如下:
{"orderCode":"1401160935542399","creator":"\u751f\u6d3b\u7528\u54c1\u6d4b\u8bd5\u5c0f\u5c4b"}
其中"creator":"\u751f\u6d3b\u7528\u54c1\u6d4b\u8bd5\u5c0f\u5c4b"为中文,目前我们用的是UTF8的。但是JAVA方接收这种的时候自动把转义后的中文转回成中文的,那签名计算方式是按这个来的,签名自然对不上了。
查了PHP手册,5.4以下的不能不转义中文,但是我们服务器上的PHP版本是5.3的,于是用PHP模拟了一个JSON方法。
复制代码 代码如下:
//模拟joson中文不转义
if (version_compare(PHP_VERSION, '5.4.0') >= 0) {
function json_encode_ex($var) {
return json_encode($var, JSON_UNESCAPED_UNICODE);
}
} else {
function json_encode_ex($var) {
if ($var === null)
return 'null';
if ($var === true)
return 'true';
if ($var === false)
return 'false';
static $reps = array(
array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"', ),
array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"', ),
);
if (is_scalar($var))
return '"' . str_replace($reps[0], $reps[1], (string) $var) . '"';
if (!is_array($var))
throw new Exception('JSON encoder error!');
$isMap = false;
$i = 0;
foreach (array_keys($var) as $k) {
if (!is_int($k) || $i++ != $k) {
$isMap = true;
break;
}
}
$s = array();
if ($isMap) {
foreach ($var as $k => $v)
$s[] = '"' . $k . '":' . call_user_func(__FUNCTION__, $v);
return '{' . implode(',', $s) . '}';
} else {
foreach ($var as $v)
$s[] = call_user_func(__FUNCTION__, $v);
return '[' . implode(',', $s) . ']';
}
}
}
用的时候直接当内置函数用就行。json_encode_ex(array('钓鱼岛'=>'中国的'));还支持多维数组。
到此,关于"怎么解决php5.4以下版本json不支持不转义内容中文的问题"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.