相关疑难解决方法(0)

如何在php json_decode中解决JSON_ERROR_UTF8错误?

我正在尝试这段代码

$json = file_get_contents("http://www.google.com/alerts/preview?q=test&t=7&f=1&l=0&e");
print_r(json_decode(utf8_encode($json), true));

        //////////////

// Define the errors.
$constants = get_defined_constants(true);
$json_errors = array();
foreach ($constants["json"] as $name => $value) {
    if (!strncmp($name, "JSON_ERROR_", 11)) {
        $json_errors[$value] = $name;
    }
}

// Show the errors for different depths.
foreach (range(4, 3, -1) as $depth) {
    var_dump(json_decode($json, true, $depth));
    echo 'Last error: ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
}
Run Code Online (Sandbox Code Playgroud)

我已经尝试了很多函数,html_entities_decode,utf8_encode和解码,解码十六进制代码,但我总是得到错误"JSON_ERROR_UTF8".

我该怎么解决这个问题?

php parsing json jsonp

48
推荐指数
3
解决办法
5万
查看次数

在Laravel中,'格式错误的UTF-8字符,可能是错误编码的'

我正在使用Laravel(一个PHP框架)为移动设备编写服务并以JSON格式返回数据.在数据结果中有一些字段编码UTF-8.

以下声明

return JsonResponse::create($data); 
Run Code Online (Sandbox Code Playgroud)

返回以下错误

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());
        }
Run Code Online (Sandbox Code Playgroud)

我改变了:

return JsonResponse::create($data);
Run Code Online (Sandbox Code Playgroud)

return JsonResponse::create($data, 200, array('Content-Type'=>'application/json; charset=utf-8' ));
Run Code Online (Sandbox Code Playgroud)

但它仍然无法正常工作.

我该如何解决?

php json utf-8 laravel-4

33
推荐指数
5
解决办法
9万
查看次数

标签 统计

json ×2

php ×2

jsonp ×1

laravel-4 ×1

parsing ×1

utf-8 ×1