Json_decode返回字符串输出


Json_decode returns string output

我正在从文本文件中读取json数据。该文件的内容包括:

{
  "id":"tag:search.twitter.com,2005:181865366610382848",
  "body":"No one wants to carry laptops any more, but we lose IPads and they are not secure. The Sweden PM ran the country for a week on an ipad.",
  "verb":"post",
  "link":"http://twitter.com/ProfNikiEllis/statuses/181865366610382848",
  "generator":{
    "link":"http://twitter.com/#!/download/ipad",
    "displayName":"Twitter for iPad"
  },
  "postedTime":"2012-03-19T22:10:56.000Z",
  "provider":{
    "link":"http://www.twitter.com",
    "displayName":"Twitter",
    "objectType":"service"
  },
  "object":{
    "summary":"No one wants to carry laptops any more, but we lose IPads and they are not secure. The Sweden PM ran the country for a week on an ipad.","id":"object:search.twitter.com,2005:181865366610382848",
    "link":"http://twitter.com/ProfNikiEllis/statuses/181865366610382848",
    "postedTime":"2012-03-19T22:10:56.000Z",
    "objectType":"note"
  }
}

我的php程序从文本文件中读取内容,并使用json_decode获取各个值。但是,json_decode给了我字符串输出而不是数组输出。请帮忙!!

$file="gist.txt";
//Convert json output to array output
$string = file_get_contents($file);
$json_output = json_decode($string,true);
echo "Check if array: ";
echo is_array($json_output)? "true": "false";echo "<br>";
echo "Check if string: ";
echo is_string($json_output)? "true": "false";echo "<br>";
echo $json_output["id"];
echo $json_output["body"];
echo "***********************************";

我的程序的输出是:

Check if array: false
Check if string: true
{{***********************************
好的,

所以文件中的 json 代码似乎有问题。我缩短了文件的内容,现在代码可以工作了。

我刚刚在本地运行了您的代码,它为我按预期运行:

Check if array: true
Check if string: false

运行它并告诉我们您得到了什么,这可能是权限问题:

$string = file_get_contents($file);
var_dump($string);
如果

成功打开,它应该输出文件的内容,如果失败,它应该输出一个不言自明的 PHP 错误。