[英]Can't decode JSON file in php
I try to decode JSON in PHP To array. 我尝试在PHP To数组中解码JSON。 This is my code 这是我的代码
<?php
$url="file/game.json";
$result_file=file_get_contents($url);
if(!$result_file){ echo 'result_file......Failed'; }
$php_result=json_decode($result_file,true);
print_r($php_result);
if($php_result){
echo 'ok';
}
else{
echo 'error';
}
?>
AND this is my json file 这是我的json文件
[
{
"name": "ABCD",
"price": "24.36",
"platform": "<span class=\"platform battle-net\"></span>",
"region": "GLOBAL"
},
{
"name": "ABCD",
"price": "27.60",
"platform": "<span class=\"platform xbox-live\"></span>",
"region": "GLOBAL"
}
]
My result Is "error". 我的结果是“错误”。 And I don't no know how to solve this problem. 而且我也不知道如何解决这个问题。
What should I do. 我该怎么办。
The file_get_contents function returns the entire file into a string or FALSE
on failure. file_get_contents函数会在失败时将整个文件返回为字符串或FALSE
。 So I assume, here you're not getting the file string. 所以我想,这里您没有得到文件字符串。 So try like this way, 所以尝试这样
<?php
try {
$url="file/game.json";
$result_file = file_get_contents('file/game.json');
if ($result_file === false) {
// Handle the error
echo "ERROR";
}else{
$php_result=json_decode($result_file,true);
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - OK';
print_r($php_result);
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
}
} catch (Exception $e) {
// Handle exception
#echo $e->getMessage();
#echo $e->code();
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.