[英]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錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.