2014-06-06 6 views
-1

저는 Json을 처음 접했고 얼마 동안 PHP를 건드리지 않았습니다. 웹 페이지에서 현재 기상 조건을 표시하기 위해 json weather api를 사용하고 있습니다. 나는 이제 예측을 추가하기로 결정했다. 예측은 동일한 jason 파일에서 제공되지만 약간 다른 형식이므로 PHP 코드를 추출 할 수 없습니다. 예측은 현재의 상황이 아니지만 여전히 기쁨이없는 배열이기 때문에 나는 그것이라고 생각합니다.PHP로 JSON 배열 디코드

는 A는 JSON 파일에서 니펫을 : 나는 다음과 같은 코드를 사용하여 조건의 일부를 추출하는 데 성공했다

{ 
    "response": { 
    "version":"0.1", 
    "termsofService":"http://www.wunderground.com/weather/api/d/terms.html", 
    "features": { 
    "geolookup": 1 
    , 
    "conditions": 1 
    , 
    "forecast": 1 
    } 
     }, 
     "observation_location": { 
     "full":"Clifton, Bristol, ", 
     "city":"Clifton, Bristol", 
     "state":"", 
     "country":"UNITED KINGDOM", 
     "country_iso3166":"GB", 
     "latitude":"51.464230", 
     "longitude":"-2.616240", 
     "elevation":"230 ft" 
     }, 
     "estimated": { 
     }, 
     "station_id":"IBRISTOL22", 
     "observation_time":"Last Updated on June 6, 8:34 AM BST", 
     "observation_time_rfc822":"Fri, 06 Jun 2014 08:34:16 +0100", 
     "observation_epoch":"1402040056", 
     "local_time_rfc822":"Fri, 06 Jun 2014 08:38:21 +0100", 
    "feelslike_string":"56.7 F (13.7 C)", 
    "feelslike_f":"56.7", 
    "feelslike_c":"13.7", 
    "visibility_mi":"N/A", 
    "visibility_km":"N/A", 
    "solarradiation":"246", 
    "UV":"1.1","precip_1hr_string":"0.00 in (0 mm)", 
    "precip_1hr_in":"0.00", 
    "precip_1hr_metric":" 0", 
    "precip_today_string":"0.00 in (0 mm)", 
    "precip_today_in":"0.00", 
    "precip_today_metric":"0", 
    "icon":"clear", 
    "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
    "forecast_url":"http://www.wunderground.com/global/stations/03726.html", 
    "history_url":"http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID=IBRISTOL22", 
    "ob_url":"http://www.wunderground.com/cgi-bin/findweather/getForecast?query=51.464230,-2.616240" 
} 
    , 
"forecast":{ 
    "txt_forecast": { 
    "date":"7:36 AM BST", 
    "forecastday": [ 
    { 
    "period":0, 
    "icon":"partlycloudy", 
    "icon_url":"http://icons.wxug.com/i/c/k/partlycloudy.gif", 
    "title":"Friday", 
    "fcttext":"Partly to mostly cloudy. High 74F. Winds ESE at 10 to 20 mph.", 
    "fcttext_metric":"Mostly cloudy skies early, then partly cloudy this afternoon. High 23C. Winds ESE at 15 to 30 kph.", 
    "pop":"10" 
    } 
    , 
    { 
    "period":1, 
    "icon":"nt_rain", 
    "icon_url":"http://icons.wxug.com/i/c/k/nt_rain.gif", 
    "title":"Friday Night", 
    "fcttext":"Partly cloudy skies early then becoming cloudy with occasional rain and a rumble or two of thunder late. Low around 60F. Winds ESE at 10 to 15 mph. Chance of rain 90%.", 
    "fcttext_metric":"Partly cloudy skies this evening will give way to periods of rain and possibly a thunderstorm overnight. Low 15C. Winds ESE at 15 to 25 kph. Chance of rain 90%.", 
    "pop":"90" 
    } 
    , 
    { 
    "period":2, 
    "icon":"rain", 
    "icon_url":"http://icons.wxug.com/i/c/k/rain.gif", 
    "title":"Saturday", 
    "fcttext":"Becoming partly cloudy after some morning rain. High around 70F. Winds SSW at 10 to 20 mph. Chance of rain 90%. Rainfall around a quarter of an inch.", 
    "fcttext_metric":"Rain early. A mix of sun and clouds in the afternoon. High 21C. Winds SSW at 15 to 25 kph. Chance of rain 90%. Rainfall around 6mm.", 
    "pop":"90" 
    } 
    , 
    { 
    "period":3,........ 

.

$json_string = file_get_contents("http://api.wunderground.com/api/XXXXXAPI-CodeXXXXX/geolookup/conditions/forecast/q/pws:ibristol22.json"); 
    $parsed_json = json_decode($json_string); 
    $feelslike = $parsed_json->{'current_observation'}->{'feelslike_c'}; 

    echo "<strong>Feels Like:</strong> ${feelslike} <br>"; 

나는 추출물 'fcttext_metric'시대 말할 필요가 무엇 PHP 코드 '0' '예측'의 -> 'forecastday'$parsed_json->forecast->txt_forecast->forecastday 통해

답변

0

루프 :

foreach ($parsed_json->forecast->txt_forecast->forecastday as $forecastday) 
{ 
    if ($forecastday->period == 0) 
    { 
     echo('fcttext_metric=' . $forecastday->fcttext_metric); 
     // ...(do something else)... 
    } 
} 

또는, 동일한 순서의 출력 데이터가 항상있는 경우에는 다음과 같이 가정합니다.

echo('fcttext_metric=' . $parsed_json->forecast->txt_forecast->forecastday[0]->fcttext_metric); 

데이터 w ith period: 0은 항상 forecastday의 첫 번째 요소입니다.

+0

안녕하십니까, 답장을 보내 주셔서 감사합니다.하지만 이러한 솔루션 중 하나를 사용할 수 없습니다. 나는 이것을 시도했다 : foreach ($ parsed_json-> forecast-> forecastday> $ forecastday) { echo "1"; } 및 1이 표시되지 않습니다. – user3714154

+0

내 게시물을 업데이트했습니다 -'txt_forecast' 키가 누락되었습니다 ... – mlask