0

特定の日付 (2013-10-14 など) の tempMaxC を取得したいこの json 結果があります。

私はこれを試しましたが、うまくいきません。

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

printf 構文については、 http : //php.net/manual/en/function.printf.phpを参照してください。

日をまたぐループ:

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 に答える