I have an array item with a French accent ([WIPDescription] => Recette Soupe à lOignon Sans Boeuf US). The data is being properly pulled from the database (mysql).

However, when I try to encode this as json using the php built in json_encode it produces a null json value (OS X server: php 5.3.4, json 1.2.1 enabled).

在Linux服务器中,描述在第一个重音字符之后被截断.

I tried all the json_encode options with no success. Any suggestions?

非常感谢.

推荐答案

json_encode只要utf-8.根据您的字符集,您可以在变量上使用iconvutf8_encode before调用json_encode.可能是array_walk_recursive.

As requested, an unfinished way to alter an array, with the assumptions that (1) it doesn't contain objects, and (2) the array keys are in ascii / lower bounds, so can be left as is:

$current_charset = 'ISO-8859-15';//or what it is now
array_walk_recursive($array,function(&$value) use ($current_charset){
     $value = iconv('UTF-8//TRANSLIT',$current_charset,$value);

});

Json相关问答推荐

如何创建可由Gin序列化到json的排序键值映射?

使用自定义类型在Golang中解析JSON数组

用巨大的值更新SQL Server中的nvarchar(max)

将 REST API - json 输出转换为表 Power BI

JOLT转换并向输出添加新数组

使用不同行数的数据创建嵌套Jolt

提交后使用 Rails 7 结合 JSON 标签进行标记

自定义将 struct 解组为切片映射

当值包含ansible中的字符串时解析json值

如何使用 ConfigurationBuilder 解析现有的 json 字符串(不是文件)

如何在返回对象的 Spring MVC @RestController @ResponseBody 类中响应 HTTP 状态代码?

apple-app-site-association json 文件是否会在应用程序中更新?

使用 gson 反序列化对象的特定 JSON 字段

JSON 使用 simplejson 序列化 Django 模型

Java循环遍历Json数组?

请求返回字节,我无法解码它们

如何在 Java 中将 YAML 转换为 JSON?

如何在dart Flutter 中将json字符串转换为json对象?

使用 axios 在 POST multipart/form-data 请求中发送文件和 json

有没有一种快速的方法可以在文本编辑器中将 JavaScript 对象转换为有效的 JSON?