Home Backend Development PHP Tutorial php中json_encode中文编码问题分析_PHP

php中json_encode中文编码问题分析_PHP

Jun 01, 2016 pm 12:14 PM
json_encode Chinese encoding

JSON

例如:'胥'经过json_encode处理后变为'\u80e5',最终的json中中文部分被替换为unicode编码。我们要解决的就是将对象转换为json并保证对象内部的中文在json中仍然是以正常的中文出现,现在看来只使用json_encode是不能达到目的的。
  我的解决方法:先将类中的中文字段进行url编码(urlencode),然后再对对象进行json编码(jsonencode),最后url解码(urldecode)json,即最终的json,里面的中文依旧是那个中文!
测试代码如下:
复制代码 代码如下:
class myClass {
public $item1 = 1;
public $item2 = '中文';
function to_json() {
//url编码,避免json_encode将中文转为unicode
$this->item2 = urlencode($this->item2);
$str_json = json_encode($this);
//url解码,转完json后将各属性返回,确保对象属性不变
$this->item2 = urldecode($this->item2);
return urldecode($str_json);
}
}
$c = new myClass();
echo json_encode($c);
echo '
';
echo $c->to_json();
echo '
';
echo json_encode($c);
echo '
';
echo json_encode('胥');
?>

程序输出结果:
复制代码 代码如下:
{"item1":1,"item2":"\u4e2d\u6587"}
{"item1":1,"item2":"中文"}
{"item1":1,"item2":"\u4e2d\u6587"}
"\u80e5"

希望本文起到抛砖引玉的作用,收集大家更好的解决方法……!
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to deal with Chinese encoding issues of file paths in Java development How to deal with Chinese encoding issues of file paths in Java development Jun 29, 2023 pm 05:11 PM

Dealing with Chinese encoding issues in file paths is a common challenge in Java development, especially when it comes to operations such as file uploading, downloading, and processing. Since Chinese characters may have different representations under different encoding methods, if they are not processed correctly, problems such as garbled characters or unrecognizable paths may occur. This article will discuss how to correctly handle the Chinese encoding problem of file paths in Java development. First, we need to understand how coding is done in Java. Java internally uses the Unicode character set to represent characters. and

How to implement encoding and decoding of Chinese characters in C language programming? How to implement encoding and decoding of Chinese characters in C language programming? Feb 19, 2024 pm 02:15 PM

In modern computer programming, C language is one of the most commonly used programming languages. Although the C language itself does not directly support Chinese encoding and decoding, we can use some technologies and libraries to achieve this function. This article will introduce how to implement Chinese encoding and decoding in C language programming software. First, to implement Chinese encoding and decoding, we need to understand the basic concepts of Chinese encoding. Currently, the most commonly used Chinese encoding scheme is Unicode encoding. Unicode encoding assigns a unique numeric value to each character so that when calculating

Common causes and solutions for Chinese garbled characters in PHP Common causes and solutions for Chinese garbled characters in PHP Mar 16, 2024 am 11:51 AM

Common causes and solutions for PHP Chinese garbled characters. With the development of the Internet, Chinese websites play an increasingly important role in our lives. However, in PHP development, the problem of Chinese garbled characters is still a common problem that troubles developers. This article will introduce the common causes of Chinese garbled characters in PHP and provide solutions. It also attaches specific code examples for readers' reference. 1. Common reasons: Inconsistent character encoding: Inconsistencies in PHP file encoding, database encoding, HTML page encoding, etc. may lead to Chinese garbled characters. database

How to correctly handle Chinese character encoding in Tomcat How to correctly handle Chinese character encoding in Tomcat Dec 28, 2023 pm 01:20 PM

How to correctly handle Chinese character encoding in Tomcat During the web development process, we often encounter the problem of handling Chinese character encoding. As a commonly used JavaWeb server, Tomcat also has some things to pay attention to when dealing with Chinese character encoding. This article will introduce how to correctly handle Chinese character encoding in Tomcat and provide corresponding code examples. 1. Understanding character encoding First, we need to understand the concept of character encoding. Character encoding is a way of mapping characters into binary data. Common

Share tips and experiences on how to deal with garbled Chinese characters in matplotlib Share tips and experiences on how to deal with garbled Chinese characters in matplotlib Jan 13, 2024 pm 02:14 PM

Tips and experience sharing on solving matplotlib Chinese garbled characters [Introduction] When using matplotlib to draw graphics, we will inevitably encounter the problem of Chinese garbled characters. This problem usually occurs in legends, axis labels, etc. In order to solve this problem, this article will share some practical tips and experiences to help readers easily solve the problem of Chinese garbled characters in matplotlib. [Problem Description] When using matplotlib to draw graphics, we use the English character set by default. Adding Chinese

Solve the problem of Chinese encoding of URL in Java development and control it within 30 words Solve the problem of Chinese encoding of URL in Java development and control it within 30 words Jun 30, 2023 pm 01:25 PM

How to solve the problem of Chinese encoding and decoding of URLs in Java development. In Java development, we often encounter the need to process URL links. Especially when dealing with Chinese characters, since the URL is a special character set, encoding and decoding operations are required. This article will introduce how to solve the problem of URL Chinese encoding and decoding in Java development. 1. URL encoding URL encoding converts non-English letters, numbers and special characters in the URL into a percent sign followed by a hexadecimal number. This is to ensure that the URL link

How to convert array to JSON format using json_encode function in PHP How to convert array to JSON format using json_encode function in PHP Jun 26, 2023 pm 12:46 PM

As a server-side programming language, PHP can easily process transmitted data. Among them, JSON format has been widely used in data transmission. But how to convert a PHP array into a JSON formatted string? At this time, PHP's json_encode function will be used. 1. What is JSON format? JSON (JavaScriptObjectNotation) is a lightweight data exchange format. It is more concise compared to XML

Convert variables to JSON format string using PHP function 'json_encode' Convert variables to JSON format string using PHP function 'json_encode' Jul 24, 2023 am 10:42 AM

Use the PHP function "json_encode" to convert variables into JSON format strings. When using PHP to develop websites or applications, you often need to convert variables into JSON format strings to facilitate data transmission and processing on the front end. PHP provides a very convenient function "json_encode" to implement this function. The "json_encode" function can convert PHP arrays or objects into JSON format strings. Below is some sample code,

See all articles