我一直试图解析一些JSON来获取一段数据。我是JSON的新手,但我想我正在从字典中解析数据,这个数据本身是从字典中选择的。
我尝试使用以下PHP从下面的示例解析度量标准温度:
$temp_c = $parsed_json->{'hourly_forecast'}[0]->{'FCTTIME'}->{'temp'}->{'metric'};
我使用2
作为定期API响应的第3个条目,但对于此摘录,它在技术上是0
。
(长)样本是这样的:
{
"response": {
"version":"0.1",
"termsofService":"http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"hourly": 1
}
}
,
"hourly_forecast": [
{
"FCTTIME": {
"hour": "23","hour_padded": "23","min": "00","min_unpadded": "0","sec": "0","year": "2018","mon": "2","mon_padded": "02","mon_abbrev": "Feb","mday": "27","mday_padded": "27","yday": "57","isdst": "0","epoch": "1519772400","pretty": "11:00 PM GMT on February 27, 2018","civil": "11:00 PM","month_name": "February","month_name_abbrev": "Feb","weekday_name": "Tuesday","weekday_name_night": "Tuesday Night","weekday_name_abbrev": "Tue","weekday_name_unlang": "Tuesday","weekday_name_night_unlang": "Tuesday Night","ampm": "PM","tz": "","age": "","UTCDATE": ""
},
"temp": {"english": "34", "metric": "1"},
"dewpoint": {"english": "21", "metric": "-6"},
"condition": "Mostly Cloudy",
"icon": "mostlycloudy",
"icon_url":"http://icons.wxug.com/i/c/k/nt_mostlycloudy.gif",
"fctcode": "3",
"sky": "76",
"wspd": {"english": "15", "metric": "24"},
"wdir": {"dir": "ENE", "degrees": "75"},
"wx": "Mostly Cloudy",
"uvi": "0",
"humidity": "60",
"windchill": {"english": "24", "metric": "-4"},
"heatindex": {"english": "-9999", "metric": "-9999"},
"feelslike": {"english": "24", "metric": "-4"},
"qpf": {"english": "0.0", "metric": "0"},
"snow": {"english": "0.0", "metric": "0"},
"pop": "21",
"mslp": {"english": "30.21", "metric": "1023"}
}
,
{
"FCTTIME": {
等
对于任何含糊不清或错误的道歉,我很乐意澄清任何事情。提前谢谢。
答案 0 :(得分:0)
看起来FCTTIME
部分是您的错误。 temp
是FCTTIME
的兄弟姐妹,而不是其子女。
此外,您不需要对这些键使用复杂(大括号)语法。对于那些无效的属性名称,例如以数字开头的字符串等,这才真正需要。
这应该有效:
$temp_c = $parsed_json->hourly_forecast[0]->temp->metric;