2014-10-06 52 views
0

我可能花了整整一天的時間來解決這個問題。我已經在堆棧上閱讀了多個問題,也一直在閱讀文章和檢查文檔,但我似乎無法弄清楚爲什麼這批代碼只生成空輸出。我是否錯過了方括號,調用了錯誤的東西?PHP解碼JSON - 空輸出

<?php 
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial"; 
$str = file_get_contents($url); 
$json = json_decode($str); 
$temp = $json['main']['temp_min']; 
$content = $temp; 


$array = array(
    "content" => $content, 
     refresh_requency => 30 

); 


echo json_encode($array); 
?> 

我再次問的是,有人可以指點我或告訴我我做錯了什麼。它是我的服務器,只是沒有正確處理數據?這可能是一種可能性。

我試過的另一件事是打印出$temp和/或其他變量如$str。當我這樣做,雖然他們甚至沒有出現,所以這就是我認爲我的問題只是不知道如何解決它。

更新

我來,這是我的網絡託管服務的結論。好像我加var_dump($json)我得到空輸出null輸出。

此外,要確認它的我的虛擬主機,如果我運行error_reporting(E_ALL); ini_set('display_errors', 1);它指向文件php.ini不允許傳出連接。我編輯我的本地家庭服務器(樹莓派)相同的文件運行相同的文件,它工作正常。

+0

http://php.net/manual /en/function.json-decode.php讀取'json_decode()'的參數,如果你想解碼爲數組,則將第二個參數設置爲true。 – 2014-10-07 03:52:00

回答

0
$json = json_decode($str, true); 

您需要第二個參數來將json字符串轉換爲關聯數組而不是對象。而你正在嘗試使用數組($json['main']['temp_min']),而不是對象。此外

$array = array(
    "content" => $content, 
    "refresh_requency" => 30 
); 

代碼看起來像

<?php 
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial"; 
$str = file_get_contents($url); 
$json = json_decode($str, true); 
$content = $json['main']['temp_min']; 

$array = array(
"content" => $content, 
"refresh_requency" => 30 
); 

echo json_encode($array); 

而且結果是http://codepad.viper-7.com/euBNAk:這樣

$temp = $json->main->temp_min; 

{"content":44.6,"refresh_requency":30} 
+1

再次,我已經在上面添加了'$ str,true)',它現在或現在都沒有什麼不同。 – pears456 2014-10-06 04:20:08

+0

Eveything工程! http://codepad.viper-7.com/euBNAk – Cheery 2014-10-06 04:22:50

+0

必須是我的服務器作爲另一個答案的評論者建議var_dump($ json),併產生一個NULL輸出。不知道我的網站託管服務有什麼問題。 – pears456 2014-10-06 04:25:19

1

訪問$臨時,你會得到所需的輸出。

另外,您需要在php.ini配置文件中允許allow_url_fopen。有些主機出於安全原因不允許它

+0

其實我已經試過了,現在你已經建議了,而且我早些時候嘗試過了,它沒有什麼區別 – pears456 2014-10-06 04:19:13

+0

ok var_dump($ json);並檢查它是否有內容 – 2014-10-06 04:23:07

+0

不允許有內容。我假設這是我的網絡主機提供商的問題。 – pears456 2014-10-06 04:25:46

0

json_decode()的第二個參數是assoc(關聯)。默認情況下它是0.當它是0(默認)時,json_decode()將返回一個對象,而不是一個數組。這就是爲什麼你無法通過使用$ json ['main'] ['temp_min']來訪問temp_min;

但是,如果您將值1用作第二個參數,則該函數將返回一個數組。參數1表示將關聯設置爲1(真)。所以使用$ json = json_decode($ str,true);而不是$ json = json_decode($ str);.您將能夠訪問$ json ['main'] ['temp_min'];現在。

你也忘了第13行(refresh_requency)的雙引號。祝你好運。

<?php 

$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial"; 
$str = file_get_contents($url); 
$json = json_decode($str, true); 

$temp = $json['main']['temp_min']; 
$content = $temp; 


$array = array(
    "content" => $content, 
    "refresh_requency" => 30 
); 

echo json_encode($array); 
0

只是想幫助

我改變你這樣的代碼,並且是正確

<?php 
    $url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial"; 
    $str = file_get_contents($url); 
    $json = json_decode($str, TRUE); // Wrong here 
    $temp = $json['main']['temp_min']; 
    $content = $temp; 

    $array = array(
     "content" => $content, 
     "refresh_requency" => 30   // And wrong here, it's must string 
    ); 

    echo json_encode($array); 
?> 
有關JSON解碼在PHP

更多信息數組或對象http://php.net/manual/en/function.json-decode.php

1

下面是爲您的上述代碼提供工作解決方案:

<?php 
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial"; 
$str = file_get_contents($url); 
$json = json_decode($str, true); 
$temp = $json['main']['temp_min']; 
$content = $temp; 


$array = array(
    "content" => $content, 
     "refresh_requency" => 30 

); 


echo json_encode($array); 
?> 

當我執行你的代碼,我發現2問題到你的代碼片段:

1)你正在嘗試使用類型stdClass的對象爲數組。

解決方案:

<?php 
$url = "http://api.openweathermap.org/data/2.5/weather?id=4879890&units=imperial"; 
$str = file_get_contents($url); 
$json = json_decode($str); 
$temp = $json->main->temp_min; 
$content = $temp; 


$array = array(
    "content" => $content, 
     "refresh_requency" => 30 

); 


echo json_encode($array); 
?> 

2)你沒有把數組鍵進入報價:

$array = array(
    "content" => $content, 
     refresh_requency => 30 

); 

它應該是:

$array = array(
    "content" => $content, 
     "refresh_requency" => 30 

);