使用PHP从JSON中提取数据
我试图解析来自WundergroundJSON的一些数据,而不使用foreach循环,只在第一天。来自此JSON的示例: 我只想得到第一天:使用PHP从JSON中提取数据,php,json,Php,Json,我试图解析来自WundergroundJSON的一些数据,而不使用foreach循环,只在第一天。来自此JSON的示例: 我只想得到第一天: $json_string = file_get_contents('http://api.wunderground.com/api/f429b85619ed45e8/geolookup/conditions/forecast/q/Australia/Sydney.json'); $parsed_json = json_decode($json_string
$json_string = file_get_contents('http://api.wunderground.com/api/f429b85619ed45e8/geolookup/conditions/forecast/q/Australia/Sydney.json');
$parsed_json = json_decode($json_string);
$weekday = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday[0]'}->{'date'}->weekday;
我在谷歌上搜索过很多,也尝试过很多例子,但通常我都会出错或一无所获。
帮助?您访问对象上的方法时出错: 由于我不确定您想要获得什么属性,下面是如何确定变量中的内容:
var_dump($parsed_json->forecast);
(那里没有“simpleforcast”。你走对了方向。只是在forecastday中获取对象之后,需要对数组元素求值
<?php
$json_string = file_get_contents('http://api.wunderground.com/api/f429b85619ed45e8/geolookup/conditions/forecast/q/Australia/Sydney.json');
$parsed_json = json_decode($json_string);
echo $weekday = $parsed_json->{'forecast'}->{'simpleforecast'}->{'forecastday'}[0]->{'date'}->weekday;
?>
$parsed_json->forecast->simpleforecast->forecastday[0]->date->weekday
?啊,好了,开始工作了。有什么区别?forecastday
是一个数组。当您放置{'forecastday[0]}
时,就好像名称是forecastday[0]
。名称实际上是forecastday
,数组索引是0
。