I'm using Laravel (a PHP framework) to write a service for mobile and have the data returned in JSON
format. In the data result there are some fields encoded in UTF-8
.
我正在使用Laravel(一个PHP框架)为移动设备编写服务,并以JSON格式返回数据。在数据结果中,有一些字段以UTF-8编码。
The following statement
以下声明
return JsonResponse::create($data);
returns the error below
返回以下错误
InvalidArgumentException
HELP
Malformed UTF-8 characters, possibly incorrectly encoded
Open: /var/www/html/vendor/symfony/http-foundation/Symfony/Component/HttpFoundation/JsonResponse.php
} catch (\Exception $exception) {
restore_error_handler();
throw $exception;
}
if (JSON_ERROR_NONE !== json_last_error()) {
throw new \InvalidArgumentException($this->transformJsonError());
}
I've changed:
我改变了:
return JsonResponse::create($data);
to
至
return JsonResponse::create($data, 200, array('Content-Type'=>'application/json; charset=utf-8' ));
but it still isn't working.
但它仍然无法正常工作。
How can I fix it?
我该如何解决?
5 个解决方案
#1
27
I wrote this method, to handle UTF8 arrays and JSON problems. It work fine with array (simple and multidimensional).
我编写了这个方法来处理UTF8数组和JSON问题。它适用于数组(简单和多维)。
/**
* Encode array from latin1 to utf8 recursively
* @param $dat
* @return array|string
*/
public static function convert_from_latin1_to_utf8_recursively($dat)
{
if (is_string($dat)) {
return utf8_encode($dat);
} elseif (is_array($dat)) {
$ret = [];
foreach ($dat as $i => $d) $ret[ $i ] = self::convert_from_latin1_to_utf8_recursively($d);
return $ret;
} elseif (is_object($dat)) {
foreach ($dat as $i => $d) $dat->$i = self::convert_from_latin1_to_utf8_recursively($d);
return $dat;
} else {
return $dat;
}
}
// Sample use
// Just pass your array or string and the UTF8 encode will be fixed
$data = convert_from_latin1_to_utf8_recursively($data);
#2
8
I found the answer to this problem here
我在这里找到了这个问题的答案
Just do
做就是了
mb_convert_encoding($data['name'], 'UTF-8', 'UTF-8');
#3
0
In my case, this causes error:
在我的情况下,这会导致错误:
return response->json(["message" => "Model status successfully updated!", "data" => $model], 200);
but this not:
但这不是:
return response->json(["message" => "Model status successfully updated!", "data" => $model->toJson()], 200);
#4
0
In my case I had a ucfirst
on the asian letters string. This was not possible and produced a non utf8 string.
在我的情况下,我在亚洲字母字符串上有一个ucfirst。这是不可能的,并产生一个非utf8字符串。
#5
-2
This error also happens if you do not activate your Mysql database or Oracle etc ...
如果您不激活Mysql数据库或Oracle等,也会发生此错误...
#1
27
I wrote this method, to handle UTF8 arrays and JSON problems. It work fine with array (simple and multidimensional).
我编写了这个方法来处理UTF8数组和JSON问题。它适用于数组(简单和多维)。
/**
* Encode array from latin1 to utf8 recursively
* @param $dat
* @return array|string
*/
public static function convert_from_latin1_to_utf8_recursively($dat)
{
if (is_string($dat)) {
return utf8_encode($dat);
} elseif (is_array($dat)) {
$ret = [];
foreach ($dat as $i => $d) $ret[ $i ] = self::convert_from_latin1_to_utf8_recursively($d);
return $ret;
} elseif (is_object($dat)) {
foreach ($dat as $i => $d) $dat->$i = self::convert_from_latin1_to_utf8_recursively($d);
return $dat;
} else {
return $dat;
}
}
// Sample use
// Just pass your array or string and the UTF8 encode will be fixed
$data = convert_from_latin1_to_utf8_recursively($data);
#2
8
I found the answer to this problem here
我在这里找到了这个问题的答案
Just do
做就是了
mb_convert_encoding($data['name'], 'UTF-8', 'UTF-8');
#3
0
In my case, this causes error:
在我的情况下,这会导致错误:
return response->json(["message" => "Model status successfully updated!", "data" => $model], 200);
but this not:
但这不是:
return response->json(["message" => "Model status successfully updated!", "data" => $model->toJson()], 200);
#4
0
In my case I had a ucfirst
on the asian letters string. This was not possible and produced a non utf8 string.
在我的情况下,我在亚洲字母字符串上有一个ucfirst。这是不可能的,并产生一个非utf8字符串。
#5
-2
This error also happens if you do not activate your Mysql database or Oracle etc ...
如果您不激活Mysql数据库或Oracle等,也会发生此错误...