0

我有这个 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"
            }
        ]
    }
}
4

3 回答 3

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 );
}

演示

于 2013-10-13T09:10:51.610 回答
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'} );
}
于 2013-10-13T09:11:21.630 回答
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 );

     }


    }
于 2013-10-13T09:14:06.717 回答