转载地址:http://www.nowamagic.net/php/php_FunctionJsonEncode.php
在 php 中使用 json_encode() 内置函数(php > 5.2)可以使用得 php 中数据可以与其它语言很好的传递并且使用它。
这个函数的功能是将数值转换成json数据存储格式。
08 |
$jsonencode = json_encode( $arr ); |
程序运行结果如下:
json_encode 函数中中文被编码成 null 了,Google 了一下,很简单,为了与前端紧密结合,Json 只支持 utf-8 编码,我认为是前端的 Javascript 也是 utf-8 的原因。
4 |
'title' =>iconv( 'gb2312' , 'utf-8' , '这里是中文标题' ), |
8 |
echo json_encode( $array ); |
这个程序的运行结果为:
1 |
{ "title" : "\u8fd9\u91cc\u662f\u4e2d\u6587\u6807\u9898" , "body" : "abcd..." } |
数组中所有中文在json_encode之后都不见了或者出现\u2353等。
解决方法是用urlencode()函数处理以下,在json_encode之前,把所有数组内所有内容都用urlencode()处理一下,然用json_encode()转换成json字符串,最后再用urldecode()将编码过的中文转回来。
02 |
/************************************************************** |
04 |
* 使用特定function对数组中所有元素做处理 |
05 |
* @param string &$array 要处理的字符串 |
06 |
* @param string $function 要执行的函数 |
07 |
* @return boolean $apply_to_keys_also 是否也应用到key上 |
10 |
*************************************************************/ |
11 |
function arrayRecursive(& $array , $function , $apply_to_keys_also = false) |
13 |
static $recursive_counter = 0; |
14 |
if (++ $recursive_counter > 1000) { |
15 |
die ( 'possible deep recursion attack' ); |
17 |
foreach ( $array as $key => $value ) { |
18 |
if ( is_array ( $value )) { |
19 |
arrayRecursive( $array [ $key ], $function , $apply_to_keys_also ); |
21 |
$array [ $key ] = $function ( $value ); |
24 |
if ( $apply_to_keys_also && is_string ( $key )) { |
25 |
$new_key = $function ( $key ); |
26 |
if ( $new_key != $key ) { |
27 |
$array [ $new_key ] = $array [ $key ]; |
35 |
/************************************************************** |
38 |
* @param array $array 要转换的数组 |
39 |
* @return string 转换得到的json字符串 |
42 |
*************************************************************/ |
43 |
function JSON( $array ) { |
44 |
arrayRecursive( $array , 'urlencode' , true); |
45 |
$json = json_encode( $array ); |
46 |
return urldecode( $json ); |
这次成功了,运行结果如下:
1 |
{ "Name" : "希亚" , "Age" : "20" } |
- 微信自定义菜单说php json_encode不转义中文汉字的方法
http://blog.csdn.net/qmhball/article/details/45690017 最近在开发微信自定义菜单. 接口比较简单,就是按微信要求的格式post一段json数据过去就 ...
- PHP JSON_ENCODE 不转义中文汉字的方法
ios程序中不识别读取到的JSON数据中 \u开头的数据. PHP 生成JSON的时候,必须将汉字不转义为 \u开头的UNICODE数据. 网上非常多,可是事实上都是错误的,正确的方法是在json_e ...
- 黄聪:PHP JSON_ENCODE 不转义中文汉字的方法
ios程序中不识别读取到的JSON数据中 \u开头的数据. PHP 生成JSON的时候,必须将汉字不转义为 \u开头的UNICODE数据. 网上很多,但是其实都是错误的,正确的方法是在json_enc ...
- 解决php函数json_encode转换后中文被编码为unicode
大家都知道使用函数json_encode()可以方便快捷地将数组进行json编码转换,但是如果数组值存在着中文,json_encode会将中文转换为unicode编码,例如: <?PHP $ar ...
- json_encode() 避免转换中文
json_encode() 避免转换中文 我们都知道,json_encode()可以将数据转换为json格式,而且只针对utf8编码的数据有效,而且在转换中文的时候,将中文转换成不可读的”\u***” ...
- php解决json_encode输出GB2312中文问题 (数组)
在 php 中使用 json_encode() 内置函数(php > 5.2)可以使用得 php 中数据可以与其它语言很好的传递并且使用它. 这个函数的功能是将数值转换成json数据存储格式. ...
- 织梦后台系统设置在PHP5.4环境中不能保存中文参数的解决方法
在没用PHP5.4的环境做Dede后台的时候,织梦58一直没有遇到这个问题,昨天上传一个新的模版到空间去测试发现后台的系统基本参数设置中所有的中文内容都无法保存,关于这个问题,其实以前也听说过,知识一 ...
- json_encode转义中文问题
默认情况下php的 json_decode 方法会把特殊字符进行转义,还会把中文转为Unicode编码形式. 这使得数据库查看文本变得很麻烦.所以我们需要限制对于中文的转义. 对于PHP5.4+版本, ...
- php5.4以下,json_encode不转义实现方法
function json_encode($input){ // 从 PHP 5.4.0 起, 增加了这个选项. if(defined('JSON_UNESCAPED_UNICODE')){ retu ...
随机推荐
- easyui —— footer
前言: 使用easyui的datagrid,在最后一行加上“总计”字样,效果如下: 过程: ... <table id="dg" title="xx管理" ...
- Host ‘host_name’ is blocked
参考:http://web2.0coder.com/archives/163 之前服务器遇到了这个错误: Host ‘host_name‘ is blocked because of many con ...
- Lua脚本和C++交互(二)
上一节讲了一些基本的Lua应用,下面,我要强调一下,Lua的栈的一些概念,因为这个确实很重要,你会经常用到.熟练使用Lua,最重要的就是要时刻知道什么时候栈里面的数据是什么顺序,都是什么.如果你能熟练 ...
- django 文档
django 学习文档 https://yiyibooks.cn/xx/django_182/index.html
- codevs 5967 [SDOI2017]相关分析
[题解] /* WA://50分 last:(r-l+1)<-- (r-mid) (r-l+1)<-- (mid-l+1) now:int mid=l+r>>1; tr[l ...
- 键盘event.which属性
IE中,只有keyCode属性,而FireFox中有which和charCode属性 event.which属性对DOM原生的event.keyCode和event.charCode进行了标准化. f ...
- {sharepoint}提升 SharePoint 代码执行权限
提升 SharePoint 代码执行权限 关于如何提升 SharePoint 代码执行权限及相关知识介绍的文章我们园子里有很多, 这里给出其中两篇文章的链接,就不再啰嗦了. http://www.cn ...
- Testlink在CentOS、windows安装
有幸在CentOS\windows上都安装过Teslink程序,总结一下.如下: 一.CentOS安装: 1.安装包需要: xampp xampp-linux-x64-5.6.3-0-installe ...
- 一个Activity中使用两个layout实例
package com.sbs.aas2l; import android.app.Activity; import android.os.Bundle; import android.view.Vi ...
- 百度移动开发平台在用angularJS