php中json_encode UTF-8中文乱码的更好解决方法,
php中json_encode UTF-8中文乱码的更好解决方法,
最近在接口代码当中用到过json_encode,在网上找到说json_encode编码设置为UTF-8中文就不会乱码,经验证这办法确实是有效果的,但是不知道为什么,代码在用过一段时间之后就不太管用了。以下是自己的解决json_encode的办法。有更好的方法请分享出来吧!
第一种:
这种简单的做一个代码转换,urlcode之后再返回所需数组
我代码这样就足够了。
代码如下 复制代码
public static function encodeOperations ($array) { foreach ((array)$array as $key => $value) { if (is_array($value)) { encodeOperations($array[$key]); } else { $array[$key] = urlencode(mb_convert_encoding($value,'UTF-8','GBK')); } } return $array; }
第二种:
这种是在网上看到的,然后就是有评论说会出现无限循环的问题,但是这显然很全面的,然后我测试之后也确实有,贴这里仅供参考
代码如下
/************************************************************** * * 使用特定function对数组中所有元素做处理 * @param string &$array 要处理的字符串 * @param string $tocode 编码后 * @param string $oldcode 编码前 * @param string $function 要执行的函数 * @return boolean $apply_to_keys_also 是否也应用到key上 * @return array $array 是否也应用到key上 * @access public * *************************************************************/ function encodeOperations (&$array, $function, $tocode=false,$oldcode=false,$apply_to_keys_also = false) { foreach ($array as $key => $value) { (www.jb51.net) if (is_array($value)) { encodeOperations($array[$key], $function, $apply_to_keys_also); } else { if($tocode&&$oldcode) { if(function_exists(mb_convert_encoding)) { $value = mb_convert_encoding($value,$tocode,$oldcode); }else{ return "error"; } } $array[$key] = $function($value); } if ($apply_to_keys_also && is_string($key)) { $new_key = $function($key); if ($new_key != $key) { $array[$new_key] = $array[$key]; unset($array[$key]); } } } return $array; } /****from:http://www.bkjia.com/phper/31/66729.htm********************************************************** * * 将数组转换为JSON字符串(兼容中文) * @param array $array 要转换的数组 * @return string 转换得到的json字符串 * @access public * *************************************************************/ function JSON($array) { arrayRecursive($array, 'urlencode', true); $json = json_encode($array); return urldecode($json); }
对中文返回是null空的话我们需要进行字符转换成uft8或者先使用urlencode进行编译呼。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Methods to solve the Chinese garbled problem of PHPDompdf PHPDompdf is a tool for converting HTML documents to PDF files. It is powerful and easy to use. However, when processing Chinese content, you sometimes encounter the problem of garbled Chinese characters. This article will introduce some methods to solve the Chinese garbled problem of PHPDompdf and provide specific code examples. 1. When using font files to process Chinese content, a common problem is that Dompdf does not support Chinese content by default.

MySQL5.7 and MySQL8.0 are two different MySQL database versions. There are some main differences between them: Performance improvements: MySQL8.0 has some performance improvements compared to MySQL5.7. These include better query optimizers, more efficient query execution plan generation, better indexing algorithms and parallel queries, etc. These improvements can improve query performance and overall system performance. JSON support: MySQL 8.0 introduces native support for JSON data type, including storage, query and indexing of JSON data. This makes processing and manipulating JSON data in MySQL more convenient and efficient. Transaction features: MySQL8.0 introduces some new transaction features, such as atomic

Common reasons and solutions for Chinese garbled characters in MySQL installation MySQL is a commonly used relational database management system, but you may encounter the problem of Chinese garbled characters during use, which brings trouble to developers and system administrators. The problem of Chinese garbled characters is mainly caused by incorrect character set settings, inconsistent character sets between the database server and the client, etc. This article will introduce in detail the common causes and solutions of Chinese garbled characters in MySQL installation to help everyone better solve this problem. 1. Common reasons: character set setting

The ultimate method to solve the problem of Chinese garbled characters in PyCharm requires specific code examples. Introduction: PyCharm, as a commonly used Python integrated development environment (IDE), has powerful functions and a friendly user interface, and is loved and used by the majority of developers. However, when PyCharm processes Chinese characters, it may sometimes encounter garbled characters, which causes certain problems in development and debugging. This article will introduce how to solve the Chinese garbled problem in PyCharm and give specific code examples. 1. Set up the project

Performance optimization methods for converting PHP arrays to JSON include: using JSON extensions and the json_encode() function; adding the JSON_UNESCAPED_UNICODE option to avoid character escaping; using buffers to improve loop encoding performance; caching JSON encoding results; and considering using a third-party JSON encoding library.

Quick Start: Pandas method of reading JSON files, specific code examples are required Introduction: In the field of data analysis and data science, Pandas is one of the important Python libraries. It provides rich functions and flexible data structures, and can easily process and analyze various data. In practical applications, we often encounter situations where we need to read JSON files. This article will introduce how to use Pandas to read JSON files, and attach specific code examples. 1. Installation of Pandas

Annotations in the Jackson library control JSON serialization and deserialization: Serialization: @JsonIgnore: Ignore the property @JsonProperty: Specify the name @JsonGetter: Use the get method @JsonSetter: Use the set method Deserialization: @JsonIgnoreProperties: Ignore the property @ JsonProperty: Specify name @JsonCreator: Use constructor @JsonDeserialize: Custom logic

The problem of Chinese garbled characters in PHP web pages is that Chinese characters are displayed as garbled characters in the web page display. This situation is usually caused by inconsistent encoding or the character set is not set. Solving the problem of Chinese garbled characters in PHP web pages requires starting from many aspects. The following are some common solutions and specific code examples. Set the PHP file encoding: First make sure that the encoding of the PHP file itself is UTF-8. You can set the UTF-8 encoding when saving in the editor, or add the following code to the header of the PHP file to set the encoding: &l
