json_encode函数:特殊字符 [英] json_encode function: special characters

查看:298
本文介绍了json_encode函数:特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用json_encode对数组进行编码时,包含特殊字符的数组元素将转换为空字符串:

Elements of an array containing special characters are converted to empty strings when encoding the array with json_encode:

$arr = array ( "funds" => "ComStage STOXX®Europe 600 Techn NR ETF", "time"=>....);
$json = json_encode($arr);

在JSON编码后,元素[funds]为空.只能使用"ComStageSTOXX®Europe600 Techn NR ETF"中的特殊字符(版权,商标等)来实现.

After JSON encoding the element [funds] is null. It happens only with special characters (copyright, trademark etc) like the ones in "ComStage STOXX®Europe 600 Techn NR ETF".

有什么建议吗?

谢谢

更新:这是在填充数组之前解决问题的方法(所有名称均取自数据库):

UPDATE: This is what solved the problem prior to populating the array (all names are taken from the db):

$mysqli->query("SET NAMES 'utf8'");

推荐答案

您的输入必须编码为UTF-8或ISO-8859-1.

Your input has to be encoded as UTF-8 or ISO-8859-1.

http://www.php.net/manual/en /function.json-encode.php

因为如果您尝试转换非utf8字符数组,您将获得0作为返回值.

Because if you try to convert an array of non-utf8 characters you'll be getting 0 as return value.

从5.5.0开始,失败时的返回值从空字符串更改为FALSE.

这篇关于json_encode函数:特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆