我正在从文本文件中读取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
{{***********************************
答案 0 :(得分:1)
好的,所以文件中的json代码似乎有问题。 我缩短了文件的内容,现在代码工作了。
答案 1 :(得分:0)
我只是在本地运行你的代码,它正按预期运行:
Check if array: true
Check if string: false
运行它并告诉我们你得到了什么,它可能是一个权限:
$string = file_get_contents($file);
var_dump($string);
如果文件成功打开,它应该输出文件的内容,如果失败则输出自解释的PHP错误。