如何在php中使用json数据的aceess元素

时间:2016-05-17 23:45:36

标签: php json

我想从下面的json数据中访问'high'元素。我在代码中插入了一些注释,以突显我想要访问的内容。

{
  "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数据,我想从中提取一个元素。

1 个答案:

答案 0 :(得分:0)

如果要访问JSON中的数组元素,则需要指定索引。你的儿子没有被显示,因为["forecast day"]是一个数组。选择第一个元素:["forecastday"][0],或循环遍历它以获取所有数组项。