我有一个名为 main.config 的文件,其中包含:
{"name":"wtf","image":"main.PNG","desc":"This is about this that and that.","tags":{"php","js","html"}}
现在在php中我正在阅读此文件并获取其内容:
$string = "";
$handle = fopen("main.config", "r");
while(!feof($handle)){
$string .= fgets($handle);
}
fclose($handle);
现在我一直在尝试对此进行解码,但它始终返回null
或带有附加""
的字符串。
"{"name":"wtf","image":"main.PNG","desc":"This is about this that and that.","tags":{"php","js","html"}}"
这就是我尝试的一切:
json_decode( preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string), true );
json_decode(preg_replace('/\s+/', '',$string), true);
json_decode(preg_replace('/\x{FEFF}/u', '', $string), true);
json_decode(str_replace('"', '"', $string));
utf8_encode($string);
stripslashes($string);
trim($string);
html_entity_decode($string);
json_decode(print_r($string, true), true);
但似乎没有任何效果,是因为我使用了一些不同的文件类型或它可能是什么?
答案 0 :(得分:1)
它只是一个无效的json格式化字符串。
更改为:
{"name":"wtf","image":"main.PNG","desc":"This is about this that and that.","tags":["php","js","html"]}
您还可以使用以下代码阅读代码较少的内容:
json_decode(file_get_contents('main.config', true);
如果您想验证json格式,您也可以使用此网站:jsonlint.com