我想從一個格式良好的json文件中檢索具有多維數組中的值的值。天氣預報在我的情況。如何從json文件中取回值?
這是我正在處理的文件的示例。 PasteBin(我很抱歉,該文件需要很長時間才能粘貼到此處)。
這是我使用的代碼,但它絕對沒有返回。
<?php
// Get Forecasts
$json_string = file_get_contents("http://weather.mailchips.com/weather.json");
$parsed_json = json_decode($json_string);
$forecasts = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'};
foreach ($forecasts as $forecast) { ?>
<div class="eachforecast">
<div class="fcday"><?php echo $forecast->{'title'};?></div>
<div class="fcicon"><?php echo $forecast->{'icon_url'};?></div>
<div class="fctext"><?php echo $forecast->{'fcttext_metric'};?></div>
</div>
<?php
}
?>
請幫幫我。我在php中的知識不足以完成這項工作。
謝謝
代碼工作正常,我。檢查您是否在php中設置了allow_url_fopen設置。嘗試添加'回聲$ json_string;',看看JSON是從服務器 –
正確服用爲什麼你使用'$ parsed_json - > {「預測」}'?這應該工作:'$ parsed_json-> forecast'如果該字段可用。 – NDM
@JerzyZawadzki是的,代碼突然開始工作。感謝Nicky De Maeyer,謝謝你的提示。 – Abhik