我正在尝试这段代码
$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
$title = array("dfg","sdfsdg","asfas","Sdfh","Ssdth","Csdgo");
$title_json = json_encode($title);
?>
<script>
var obj = JSON.parse('<?= $title_json; ?>');
console.log(obj);
</script>
Run Code Online (Sandbox Code Playgroud)
此代码不会:
<?php
$title = array("Is President Trump still using his old Android phone?","Trump sets a dizzying WH pace in first days","Trumps White House Charm Offensive a Contrast to Solitary Obama","The inside story of a basketball teen so tall, he doesn't look real","Scientists say they are closer to making Star Wars holograms","Sperm theft lawsuit leaves appeals court weighing how much a life …Run Code Online (Sandbox Code Playgroud)