用PHP显示JSON数据
我有以下数组用PHP显示JSON数据,php,json,Php,Json,我有以下数组 "forecast":{ "txt_forecast": { "date":"11:00 PM EDT", "forecastday": [ { "period":0, "icon":"partlycloudy", "icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif", "title":"Saturday", "fcttext":"Partly
"forecast":{
"txt_forecast": {
"date":"11:00 PM EDT",
"forecastday": [
{
"period":0,
"icon":"partlycloudy",
"icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title":"Saturday",
"fcttext":"Partly cloudy in the morning, then mostly cloudy. High of 88F. Winds from the South at 5 to 10 mph.",
"fcttext_metric":"Partly cloudy in the morning, then mostly cloudy. High of 31C. Winds from the South at 10 to 15 km/h.",
"pop":"0"
}
,
{
"period":1,
"icon":"partlycloudy",
"icon_url":"http://icons-ak.wxug.com/i/c/k/partlycloudy.gif",
"title":"Saturday Night",
"fcttext":"Overcast in the evening, then partly cloudy. Low of 64F. Winds less than 5 mph.",
"fcttext_metric":"Overcast in the evening, then partly cloudy. Low of 18C. Winds less than 5 km/h.",
"pop":"10"
}
如何在PHP中显示信息?它一直持续到第7阶段
编辑:这是我编辑过的代码,不起作用
<?php
$json_string = file_get_contents("http://api.wunderground.com/api/7ec5f6510a4656df/geolookup/forecast/q/40121.json");
$parsed_json = json_decode($json_string);
$temp = $parsed_json->{'forecast'}->{'txt_forecast'}->{'date'};
$title = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}->{'title'};
$for = $parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}->{'fcttext'};
echo "Current date is ${temp}, ${title}: ${for}\n";
foreach($parsed_json['forecast']['forecastday[0]'] as $key => $value)
{
echo $value['period'];
echo $value['icon'];
// etc
}
?>
更新
您将不得不使用该函数
$myArray = json_decode($yourJSON, true);
现在,您可以使用foreach
从$myArray
数组获取数据,例如:
foreach($myArray['forecast']['forecastday'] as $key => $value)
{
echo $value['period'];
echo $value['icon'];
// etc
}
您还可以将json转换为PHP对象:
$myData = json_decode($yourJSON);
foreach($myData->forecast->forecastday as $day) {
echo $day->period;
echo $day->icon;
}
编辑: 我认为您可以通过以下方式修复问题中的代码:
<?php
$json_string = file_get_contents("http://api.wunderground.com/api/7ec5f6510a4656df/geolookup/forecast/q/40121.json");
$parsed_json = json_decode($json_string);
$forecast = $parsed_json->forecast->txt_forecast;
$temp = $forecast->date;
$title = $forecast->forecastday->title;
$for = $forecast->forecastday->fcttext;
echo "Current date is ${temp}, ${title}: ${for}\n";
foreach($forecast->forecastday as $value) {
echo $value['period'];
echo $value['icon'];
// etc
}
?>
@Eric:要将其转换为数组,这是我个人的偏好:)@DavidMorin:请参阅更新的答案plz。您没有在问题中首先提供正确的json起点:)请注意,您的json无效:您需要将其包围在{}
中,或者删除“预测”:
。另外,你永远不会关闭方括号。看起来你想要另一种解码形式:我的回答注意,$parsed_json->{'forecast'}->{'txt_forecast'}->{'forecastday'}->{'fcttext'}
相当于$parsed_json->forecast->txt_forecast->forecastday->fcttext
我想我没有理解你的意思。。你能告诉我吗?警告:为foreach()提供的参数无效@DavidMorin:print\r($myData->forecast->forecastday)给出了什么?@DavidMorin:print\r($myData->forecast)?通过上面的编辑,我得到了解析错误:语法错误,意外的T\u变量in@DavidMorin:缺少分号
$myData = json_decode($yourJSON);
foreach($myData->forecast->forecastday as $day) {
echo $day->period;
echo $day->icon;
}
<?php
$json_string = file_get_contents("http://api.wunderground.com/api/7ec5f6510a4656df/geolookup/forecast/q/40121.json");
$parsed_json = json_decode($json_string);
$forecast = $parsed_json->forecast->txt_forecast;
$temp = $forecast->date;
$title = $forecast->forecastday->title;
$for = $forecast->forecastday->fcttext;
echo "Current date is ${temp}, ${title}: ${for}\n";
foreach($forecast->forecastday as $value) {
echo $value['period'];
echo $value['icon'];
// etc
}
?>