0

我有一个包含以下内容的 .json 文件:

{ "questions": "reponse" }

我想将文件的内容解析成一个 PHP 数组,但我有一个奇怪的问题......

$path = 'myFile.json';
echo file_get_contents($path); 
echo var_dump(json_decode(file_get_contents($path), true));
echo var_dump(json_decode(utf8_encode(file_get_contents($path), true)));
$json = '{ "questions": "reponse" }';
echo var_dump(json_decode($json, true));

我屏幕上的结果是:

{ "questions": "reponse" }
null
null
array (size=1)
  'questions' => string 'reponse' (length=7)

文件中的字符串和我程序中的字符串有什么区别?

谢谢!

4

2 回答 2

0

尝试运行此代码:

var_dump(json_decode(trim(file_get_contents($path)), true));

我希望有一些空格(如 BOM/UTF-8 标头)会造成麻烦。

于 2012-11-24T15:12:18.603 回答
0

utf8_encode()只接受一个参数。你通过两个。一旦它被修复它应该工作:

{ "questions": "reponse" }
array(1) {
  ["questions"]=>
  string(7) "reponse"
}
object(stdClass)#1 (1) {
  ["questions"]=>
  string(7) "reponse"
}
array(1) {
  ["questions"]=>
  string(7) "reponse"
}
于 2012-11-24T15:13:30.633 回答