我有这个json结果,我希望获得特定日期的tempMaxC(例如2013-10-14)。
我试过这个但是没有用。
$json=json_decode($json_reply);
printf("<p>Current temp</p>",
$json->{'data'}->{'weather'}['0']->{'date'},
$json->{'data'}->{'weather'}['0']->{'tempMaxC'} );
杰森:
{
"data": {
"current_condition": [
{
"cloudcover": "25",
"humidity": "81",
"observation_time": "08:51 AM",
"precipMM": "0.0",
"pressure": "1019",
"temp_C": "6",
"temp_F": "43",
"visibility": "10",
"weatherCode": "113",
"weatherDesc": [
{
"value": "Sunny"
}
],
"weatherIconUrl": [
{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0001_sunny.png"
}
],
"winddir16Point": "SSW",
"winddirDegree": "210",
"windspeedKmph": "9",
"windspeedMiles": "6"
}
],
"request": [
{
"query": "Adelboden, Switzerland",
"type": "City"
}
],
"weather": [
{
"date": "2013-10-13",
"precipMM": "3.2",
"tempMaxC": "7",
"tempMaxF": "44",
"tempMinC": "0",
"tempMinF": "32",
"weatherCode": "116",
"weatherDesc": [
{
"value": "Partly Cloudy"
}
],
"weatherIconUrl": [
{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png"
}
],
"winddir16Point": "SW",
"winddirDegree": "233",
"winddirection": "SW",
"windspeedKmph": "12",
"windspeedMiles": "7"
},
{
"date": "2013-10-14",
"precipMM": "0.6",
"tempMaxC": "9",
"tempMaxF": "48",
"tempMinC": "0",
"tempMinF": "32",
"weatherCode": "116",
"weatherDesc": [
{
"value": "Partly Cloudy"
}
],
"weatherIconUrl": [
{
"value": "http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png"
}
],
"winddir16Point": "SSW",
"winddirDegree": "213",
"winddirection": "SSW",
"windspeedKmph": "12",
"windspeedMiles": "8"
}
]
}
}
答案 0 :(得分:2)
您需要告诉函数printf()
如何以格式打印参数:
printf("<p>Date: %s | Temp: %s°C</p>", $arg1, $arg2);
循环几天:
foreach ($json->data->weather as $day) {
printf("<p>Date: %s | Temp: %s°C</p>", $day->date, $day->tempMaxC );
}
答案 1 :(得分:0)
您忘记在printf
功能中指定格式。即%d,%s ....
尝试
printf("<p>Current temp %s %s</p>", $json->{'data'}->{'weather'}['0']->{'date'}, $json->{'data'}->{'weather'}['0']->{'tempMaxC'} );
请参阅:http://php.net/manual/en/function.printf.php了解printf语法
循环几天:
foreach( $json->{'data'}->{'weather'} as $days )
{
printf("<p>Current temp %s %s</p>",
$days->{'date'},
$days->{'tempMaxC'} );
}
答案 2 :(得分:0)
试试这个
$json=json_decode($json_reply);
foreach($json->data->weather as $c ){
if($c->date == '2013-10-14'){
printf("<p>Date: %s | Temp: %s°C</p>", $c->date, $c->tempMaxC );
}
}