PHP json_encode输出josn格式并解决中文乱码
json 数据格式数据用得最多的是与js,flash实时交互用的,那么php怎么返回json数据格式呢,下面我来分析一下实例.
PHP实例代码如下:
<?php header('Content-type: text/json'); $fruits = array ( "fruits" => array("a" => "orange", "b" => "banana", "c" => "apple"), "numbers" => array(1, 2, 3, 4, 5, 6), "holes" => array("first", 5 => "second", "third") ); echo json_encode($fruits); ?>
上面是英文是没有问题,如果是中文就会有问题,解决办法如下.
Json 只支持 utf-8 编码,我认为是前端的 Javascript 也是 utf-8 的原因,代码如下:
<?php $array = array( 'title' => iconv('gb2312', 'utf-8', '这里是中文标题') , 'body' => 'abcd...' ); echo json_encode($array); ?>
//结果如下:
{"title":"u8fd9u91ccu662fu4e2du6587u6807u9898","body":"abcd..."}
利用js来分析这个函数,代码如下:
$(function(){ $('#send').click(function() { $.getJSON('json.php', function(data) { $('#resText').emptyempty(); var html = ''; $.each( data , function(commentIndex, comment) { html += '<div class="comment"><h6 id="nbsp-nbsp-comment-username-nbsp-nbsp">' + comment['username'] + ':</h6><p class="para">' + comment['content'] + '</p></div>'; }) $('#resText').html(html); }) }) })
注意在你的php输出js格式时我们必须是header('Content-type: text/json');这样的头部信息发送,后面加一个完整的可解析中文乱码的问题程序,代码如下:
<?php /************************************************************** * * 使用特定function对数组中所有元素做处理 * @param string &$array 要处理的字符串 * @param string $function 要执行的函数 * @return boolean $apply_to_keys_also 是否也应用到key上 * @access public * *************************************************************/ function arrayRecursive(&$array, $function, $apply_to_keys_also = false) { static $recursive_counter = 0; if (++$recursive_counter > 1000) { die('possible deep recursion attack'); } foreach ($array as $key => $value) { if (is_array($value)) { arrayRecursive($array[$key], $function, $apply_to_keys_also); } else { $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]); } } } $recursive_counter--; } /************************************************************** * * 将数组转换为JSON字符串(兼容中文) * @param array $array 要转换的数组 * @return string 转换得到的json字符串 * @access public * *************************************************************/ function JSON($array) { arrayRecursive($array, 'urlencode', true); $json = json_encode($array); return urldecode($json); } $array = array( 'Name' => '希亚', 'Age' => 20 ); echo JSON($array); ?>
本文地址:
转载随意,但请附上文章地址:-)

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.

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

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

Solutions for ajax to transmit Chinese garbled characters: 1. Set a unified encoding method; 2. Server-side encoding; 3. Client-side decoding; 4. Set HTTP response headers; 5. Use JSON format. Detailed introduction: 1. Set a unified encoding method to ensure that the server and client use the same encoding method. Under normal circumstances, UTF-8 is a commonly used encoding method because it can support multiple languages and character sets; 2 , Server-side encoding. On the server side, ensure that the Chinese data is encoded in the correct encoding method and then passed to the client, etc.

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

The problem of Linux Chinese garbled characters is a problem that many Chinese users often encounter when using Linux systems. The main reason for garbled Chinese characters is that the default character encoding used by the Linux system is UTF-8, but some software or applications may not be compatible with UTF-8 encoding, causing Chinese characters to not be displayed correctly. There are many ways to solve this problem. Several common solutions will be detailed below and specific code examples will be provided. Modify the terminal character encoding settings: The terminal character encoding settings determine whether the terminal can correctly

Practical tips for quickly solving Chinese garbled characters in Eclipse require specific code examples. Overview: Eclipse is a widely used integrated development environment (IDE) that not only supports the development of multiple programming languages, but also supports multiple operating systems. However, sometimes when using Eclipse, we may encounter the problem of Chinese garbled characters, which brings inconvenience to our development work. This article will introduce some practical techniques to help us quickly solve the problem of Chinese garbled characters in Eclipse, and attach specific code examples. one,

An effective method to quickly solve matplotlib Chinese garbled characters. Introduction: matplotlib is a commonly used drawing library in Python. However, when using Chinese for annotation and display, garbled characters often occur. This article will introduce some effective workarounds and provide specific code examples. 1. Set the font. matplotlib uses system fonts for Chinese display by default. However, system fonts often do not contain Chinese characters, so you need to manually set the appropriate Chinese font. First, you need to confirm whether the computer has
