使用php从另一个文件读取对象时出现问题

使用php从另一个文件读取对象时出现问题,php,json,Php,Json,我有一个名为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($h

我有一个名为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);

但似乎什么都不起作用,是因为我使用了不同的文件类型还是可能是什么?

它只是一个无效的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格式,你也可以使用这个网站:

你可以使用类似的东西:
$configJson=json\u decode(file\u get\u contents('main.config',true);
你的原始字符串似乎不是有效的json,只要把你的json字符串修改成正确的格式,标签数组就错了。然后使用json\u decode(file\u get\u contents)('main.config',true);数组ex:“cars”:[“Ford”,“BMW”,“Fiat”]
“tags”:{“php”,“js”,“html”}
我认为这不是有效的JSON,它应该是
“tags”:[“php”,“js”,“html”]
@Danny yeah你是对的,现在它即使没有文件内容也能工作。有理由使用文件内容而不是我的方式吗?