我正在尝试此代码
$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;
}
我尝试了很多函数,html_entities_decode,utf8_encode和解码,解码十六进制代码,但是我总是收到错误“JSON_ERROR_UTF8”。
我该如何解决?
最佳答案
There is a good function 清理数组。
我建议您使用像这样的json_encode包装器:
function safe_json_encode($value, $options = 0, $depth = 512, $utfErrorFlag = false) {
$encoded = json_encode($value, $options, $depth);
switch (json_last_error()) {
case JSON_ERROR_NONE:
return $encoded;
case JSON_ERROR_DEPTH:
return 'Maximum stack depth exceeded'; // or trigger_error() or throw new Exception()
case JSON_ERROR_STATE_MISMATCH:
return 'Underflow or the modes mismatch'; // or trigger_error() or throw new Exception()
case JSON_ERROR_CTRL_CHAR:
return 'Unexpected control character found';
case JSON_ERROR_SYNTAX:
return 'Syntax error, malformed JSON'; // or trigger_error() or throw new Exception()
case JSON_ERROR_UTF8:
$clean = utf8ize($value);
if ($utfErrorFlag) {
return 'UTF8 encoding error'; // or trigger_error() or throw new Exception()
}
return safe_json_encode($clean, $options, $depth, true);
default:
return 'Unknown error'; // or trigger_error() or throw new Exception()
}
}
function utf8ize($mixed) {
if (is_array($mixed)) {
foreach ($mixed as $key => $value) {
$mixed[$key] = utf8ize($value);
}
} else if (is_string ($mixed)) {
return utf8_encode($mixed);
}
return $mixed;
}
在我的应用程序中,utf8_encode()比iconv()更好地工作
关于php - 如何解决php json_decode中的JSON_ERROR_UTF8错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10199017/