2016-05-17 8 views
0

以下のjsonデータから 'high'要素にアクセスしたかったのです。私はアクセスしたいものをハイライトするコードにいくつかのコメントを挿入しました。私は次のことを試してみましたPHPでjsonデータの要素を修復する方法

{ 
    "response": { 
    "version":"0.1", 
    "termsofService":"http://www.wunderground.com/weather/api/d/terms.html", 
    "features": { 
    "forecast": 1 
    } 
} 
    , 
    "forecast":{ 
     "txt_forecast": { 
     "date":"1:30 AM PKT", 
     "forecastday": [ 
     { 
     "period":0, 
     "icon":"clear", 
     "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
     "title":"Wednesday", 
     "fcttext":"Sunny skies. High 114F. Winds NW at 5 to 10 mph.", 
     "fcttext_metric":"Mainly sunny. High 46C. Winds NW at 10 to 15 km/h.", 
     "pop":"0" 
     } 
    , 
    { 
    "period":1, 
    "icon":"nt_clear", 
    "icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif", 
    "title":"Wednesday Night", 
    "fcttext":"Clear. Low near 85F. Winds light and variable.", 
    "fcttext_metric":"Clear. Low 29C. Winds light and variable.", 
    "pop":"0" 
    } 
    , 
    { 
    "period":2, 
    "icon":"clear", 
    "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
    "title":"Thursday", 
    "fcttext":"Sunny. High around 115F. Winds NNE at 5 to 10 mph.", 
    "fcttext_metric":"Sunny skies. High 46C. Winds NNE at 10 to 15 km/h.", 
    "pop":"0" 
    } 
    , 
    { 
    "period":3, 
    "icon":"nt_clear", 
    "icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif", 
    "title":"Thursday Night", 
    "fcttext":"A mostly clear sky. Low 83F. Winds light and variable.", 
    "fcttext_metric":"Clear skies. Low 28C. Winds light and variable.", 
    "pop":"0" 
    } 
    , 
    { 
    "period":4, 
    "icon":"clear", 
    "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
    "title":"Friday", 
    "fcttext":"Sunny. High 116F. Winds ENE at 5 to 10 mph.", 
    "fcttext_metric":"Sunny skies. High 47C. Winds ENE at 10 to 15 km/h.", 
    "pop":"0" 
    } 
    , 
    { 
    "period":5, 
    "icon":"nt_clear", 
    "icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif", 
    "title":"Friday Night", 
    "fcttext":"A mostly clear sky. Low 84F. Winds SSE at 5 to 10 mph.", 
    "fcttext_metric":"Clear. Low 29C. Winds light and variable.", 
    "pop":"0" 
    } 
    , 
    { 
    "period":6, 
    "icon":"clear", 
    "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
    "title":"Saturday", 
    "fcttext":"A mainly sunny sky. High 117F. SE winds shifting to NNE at 10 to 15 mph.", 
    "fcttext_metric":"A mainly sunny sky. High 47C. Winds E at 10 to 15 km/h.", 
    "pop":"0" 
    } 
    , 
    { 
    "period":7, 
    "icon":"nt_clear", 
    "icon_url":"http://icons.wxug.com/i/c/k/nt_clear.gif", 
    "title":"Saturday Night", 
    "fcttext":"Clear skies. Low 88F. Winds light and variable.", 
    "fcttext_metric":"Clear skies. Low 31C. Winds light and variable.", 
    "pop":"0" 
    } 
    ] 
    }, 
    "simpleforecast": { 
    "forecastday": [ 
    {"date":{ 
"epoch":"1463580000", 
"pretty":"7:00 PM PKT on May 18, 2016", 
"day":18, 
"month":5, 
"year":2016, 
"yday":138, 
"hour":19, 
"min":"00", 
"sec":0, 
"isdst":"0", 
"monthname":"May", 
"monthname_short":"May", 
"weekday_short":"Wed", 
"weekday":"Wednesday", 
"ampm":"PM", 
"tz_short":"PKT", 
"tz_long":"Asia/Karachi" 
}, 
    "period":1, 
    "high": { **This value is to be extracted** 
    "fahrenheit":"114", 
    "celsius":"46" 
    }, 
    "low": { 
    "fahrenheit":"85", 
    "celsius":"29" 
    }, 
    "conditions":"Clear", 
    "icon":"clear", 
    "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
    "skyicon":"", 
    "pop":0, 
    "qpf_allday": { 
    "in": 0.00, 
    "mm": 0 
    }, 
    "qpf_day": { 
    "in": 0.00, 
    "mm": 0 
    }, 
    "qpf_night": { 
    "in": 0.00, 
    "mm": 0 
    }, 
    "snow_allday": { 
    "in": 0.0, 
    "cm": 0.0 
    }, 
    "snow_day": { 
    "in": 0.0, 
    "cm": 0.0 
    }, 
    "snow_night": { 
    "in": 0.0, 
    "cm": 0.0 
    }, 
    "maxwind": { 
    "mph": 10, 
    "kph": 16, 
    "dir": "NW", 
    "degrees": 316 
    }, 
    "avewind": { 
    "mph": 8, 
    "kph": 13, 
    "dir": "NW", 
    "degrees": 316 
    }, 
    "avehumidity": 17, 
    "maxhumidity": 0, 
    "minhumidity": 0 
    } 
    , 
    {"date":{ 
"epoch":"1463666400", 
"pretty":"7:00 PM PKT on May 19, 2016", 
"day":19, 
"month":5, 
"year":2016, 
"yday":139, 
"hour":19, 
"min":"00", 
"sec":0, 
"isdst":"0", 
"monthname":"May", 
"monthname_short":"May", 
"weekday_short":"Thu", 
"weekday":"Thursday", 
"ampm":"PM", 
"tz_short":"PKT", 
"tz_long":"Asia/Karachi" 
}, 
    "period":2, 
    "high": { 
    "fahrenheit":"115", 
    "celsius":"46" 
    }, 
    "low": { 
    "fahrenheit":"83", 
    "celsius":"28" 
    }, 
    "conditions":"Clear", 
    "icon":"clear", 
    "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
    "skyicon":"", 
    "pop":0, 
    "qpf_allday": { 
    "in": 0.00, 
    "mm": 0 
    }, 
    "qpf_day": { 
    "in": 0.00, 
    "mm": 0 
    }, 
    "qpf_night": { 
    "in": 0.00, 
    "mm": 0 
    }, 
    "snow_allday": { 
    "in": 0.0, 
    "cm": 0.0 
    }, 
    "snow_day": { 
    "in": 0.0, 
    "cm": 0.0 
    }, 
    "snow_night": { 
    "in": 0.0, 
    "cm": 0.0 
    }, 
    "maxwind": { 
    "mph": 10, 
    "kph": 16, 
    "dir": "NNE", 
    "degrees": 16 
    }, 
    "avewind": { 
    "mph": 6, 
    "kph": 10, 
    "dir": "NNE", 
    "degrees": 16 
    }, 
    "avehumidity": 15, 
    "maxhumidity": 0, 
    "minhumidity": 0 
    } 
    , 
    {"date":{ 
"epoch":"1463752800", 
"pretty":"7:00 PM PKT on May 20, 2016", 
"day":20, 
"month":5, 
"year":2016, 
"yday":140, 
"hour":19, 
"min":"00", 
"sec":0, 
"isdst":"0", 
"monthname":"May", 
"monthname_short":"May", 
"weekday_short":"Fri", 
"weekday":"Friday", 
"ampm":"PM", 
"tz_short":"PKT", 
"tz_long":"Asia/Karachi" 
}, 
    "period":3, 
    "high": { 
    "fahrenheit":"116", 
    "celsius":"47" 
    }, 
    "low": { 
    "fahrenheit":"84", 
    "celsius":"29" 
    }, 
    "conditions":"Clear", 
    "icon":"clear", 
    "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
    "skyicon":"", 
    "pop":0, 
    "qpf_allday": { 
    "in": 0.00, 
    "mm": 0 
    }, 
    "qpf_day": { 
    "in": 0.00, 
    "mm": 0 
    }, 
    "qpf_night": { 
    "in": 0.00, 
    "mm": 0 
    }, 
    "snow_allday": { 
    "in": 0.0, 
    "cm": 0.0 
    }, 
    "snow_day": { 
    "in": 0.0, 
    "cm": 0.0 
    }, 
    "snow_night": { 
    "in": 0.0, 
    "cm": 0.0 
    }, 
    "maxwind": { 
    "mph": 10, 
    "kph": 16, 
    "dir": "ENE", 
    "degrees": 75 
    }, 
    "avewind": { 
    "mph": 9, 
    "kph": 14, 
    "dir": "ENE", 
    "degrees": 75 
    }, 
    "avehumidity": 18, 
    "maxhumidity": 0, 
    "minhumidity": 0 
    } 
    , 
    {"date":{ 
"epoch":"1463839200", 
"pretty":"7:00 PM PKT on May 21, 2016", 
"day":21, 
"month":5, 
"year":2016, 
"yday":141, 
"hour":19, 
"min":"00", 
"sec":0, 
"isdst":"0", 
"monthname":"May", 
"monthname_short":"May", 
"weekday_short":"Sat", 
"weekday":"Saturday", 
"ampm":"PM", 
"tz_short":"PKT", 
"tz_long":"Asia/Karachi" 
}, 
    "period":4, 
    "high": { 
    "fahrenheit":"117", 
    "celsius":"47" 
    }, 
    "low": { 
    "fahrenheit":"88", 
    "celsius":"31" 
    }, 
    "conditions":"Clear", 
    "icon":"clear", 
    "icon_url":"http://icons.wxug.com/i/c/k/clear.gif", 
    "skyicon":"", 
    "pop":0, 
    "qpf_allday": { 
    "in": 0.00, 
    "mm": 0 
    }, 
    "qpf_day": { 
    "in": 0.00, 
    "mm": 0 
    }, 
    "qpf_night": { 
    "in": 0.00, 
    "mm": 0 
    }, 
    "snow_allday": { 
    "in": 0.0, 
    "cm": 0.0 
    }, 
    "snow_day": { 
    "in": 0.0, 
    "cm": 0.0 
    }, 
    "snow_night": { 
    "in": 0.0, 
    "cm": 0.0 
    }, 
    "maxwind": { 
    "mph": 15, 
    "kph": 24, 
    "dir": "E", 
    "degrees": 87 
    }, 
    "avewind": { 
    "mph": 10, 
    "kph": 16, 
    "dir": "E", 
    "degrees": 87 
    }, 
    "avehumidity": 20, 
    "maxhumidity": 0, 
    "minhumidity": 0 
    } 
    ] 
    } 
} 
} 

$def = json_decode($weather_json, true); 

echo $def['forecast']['simpleforecast']['forecastday']['date']['high'][celsius']; 

ここでは '$のweather_jsonは、' 私は要素を抽出したかったから、私の上記のJSONデータです。

答えて

0

JSONの配列要素にアクセスする場合は、インデックスを指定する必要があります。 ["forecast day"]は配列なので、あなたの息子は表示されていません。次のような最初の要素(["forecastday"][0])を選択するか、ループしてすべての配列項目を取得します。

+0

Thnxこれは私の問題を解決しました。 – Mudassir

関連する問題